ENHANCEMENT Added MultiForm->getSavedSteps() and MultiForm->getSavedStepByClass()

ENHANCEMENT Added MultiFormStep->validateStep() for custom validation routines
ENHANCEMENT Changed MultiForm->save() to use $form->getData() instead of $data to ensure that all fields are saveable into a DataObject (had trouble with ConfirmedPasswordField returning array instead of string)
This commit is contained in:
Ingo Schommer 2008-10-01 18:36:52 +00:00
parent 228d03592e
commit 7968b09fea
2 changed files with 149 additions and 87 deletions

View File

@ -83,9 +83,8 @@ abstract class MultiForm extends Form {
// Set up the actions for the current step // Set up the actions for the current step
$actions = $this->actionsFor($currentStep); $actions = $this->actionsFor($currentStep);
// Set up validation (if necessary) {@TODO find a better way instead // Set up validation (if necessary)
// of hardcoding a check for action_prev in order to prevent validation // @todo find a better way instead of hardcoding a check for action_prev in order to prevent validation when hitting the back button
// when hitting the back button
$validator = null; $validator = null;
if(empty($_REQUEST['action_prev'])) { if(empty($_REQUEST['action_prev'])) {
if($this->getCurrentStep()->getValidator()) { if($this->getCurrentStep()->getValidator()) {
@ -211,6 +210,41 @@ abstract class MultiForm extends Form {
return DataObject::get_one('MultiFormSession', "Hash = '$SQL_hash' AND IsComplete = 0"); return DataObject::get_one('MultiFormSession', "Hash = '$SQL_hash' AND IsComplete = 0");
} }
/**
* Get all steps saved in the database for the currently active session,
* in the order they were saved, oldest to newest (automatically ordered by ID).
* If you want a full chain of steps regardless if they've already been saved
* to the database, use {@link getAllStepsLinear()}.
*
* @return DataObjectSet|boolean A set of MultiFormStep subclasses
*/
function getSavedSteps() {
return DataObject::get(
'MultiFormStep',
sprintf("SessionID = '%s'",
$this->session->ID
)
);
}
/**
* Get a step which was previously saved to the database in the current session.
* Caution: This might cause unexpected behaviour if you have multiple steps
* in your chain with the same classname.
*
* @param string $className Classname of a {@link MultiFormStep} subclass
* @return MultiFormStep
*/
function getSavedStepByClass($className) {
return DataObject::get_one(
'MultiFormStep',
sprintf("SessionID = '%s' AND ClassName = '%s'",
$this->session->ID,
Convert::raw2sql($className)
)
);
}
/** /**
* Build a FieldSet of the FormAction fields for the given step. * Build a FieldSet of the FormAction fields for the given step.
* *
@ -309,16 +343,22 @@ abstract class MultiForm extends Form {
* @param object $form The form that the action was called on * @param object $form The form that the action was called on
*/ */
public function next($data, $form) { public function next($data, $form) {
if(!$this->getCurrentStep()->getNextStep()) { // Get the next step class
$nextStepClass = $this->getCurrentStep()->getNextStep();
if(!$nextStepClass) {
Director::redirectBack(); Director::redirectBack();
return false; return false;
} }
// Get the next step class // custom validation (use MultiFormStep->getValidator() for built-in functionality)
$nextStepClass = $this->getCurrentStep()->getNextStep(); if(!$this->getCurrentStep()->validateStep($data, $form)) {
Director::redirectBack();
return false;
}
// Save the form data for the current step // Save the form data for the current step
$this->save($data); $this->save($form->getData());
// Determine whether we can use a step already in the DB, or have to create a new one // Determine whether we can use a step already in the DB, or have to create a new one
if(!$nextStep = DataObject::get_one($nextStepClass, "SessionID = {$this->session->ID}")) { if(!$nextStep = DataObject::get_one($nextStepClass, "SessionID = {$this->session->ID}")) {
@ -381,7 +421,7 @@ abstract class MultiForm extends Form {
$currentStep = $this->getCurrentStep(); $currentStep = $this->getCurrentStep();
if(is_array($data)) { if(is_array($data)) {
foreach($data as $field => $value) { foreach($data as $field => $value) {
if(in_array($field, $this->stat('ignored_fields')) || self::is_action_field($field)) { if(in_array($field, $this->stat('ignored_fields'))) {
unset($data[$field]); unset($data[$field]);
} }
} }
@ -409,7 +449,7 @@ abstract class MultiForm extends Form {
/** /**
* Determine the steps to show in a linear fashion, starting from the * Determine the steps to show in a linear fashion, starting from the
* first step. We run a recursive function passing the steps found * first step. We run {@link getAllStepsRecursive} passing the steps found
* by reference to get a listing of the steps. * by reference to get a listing of the steps.
* *
* @return DataObjectSet * @return DataObjectSet
@ -436,6 +476,9 @@ abstract class MultiForm extends Form {
* Recursively run through steps using the getNextStep() method on each step * Recursively run through steps using the getNextStep() method on each step
* to determine what the next step is, gathering each step along the way. * to determine what the next step is, gathering each step along the way.
* We stop on the last step, and return the results. * We stop on the last step, and return the results.
* If a step in the chain was already saved to the database in the current
* session, its used - otherwise a singleton of this step is used.
* Caution: Doesn't consider branching for steps which aren't in the database yet.
* *
* @param $step Subclass of MultiFormStep to find the next step of * @param $step Subclass of MultiFormStep to find the next step of
* @param $stepsFound $stepsFound DataObjectSet reference, the steps found to call back on * @param $stepsFound $stepsFound DataObjectSet reference, the steps found to call back on

View File

@ -100,6 +100,7 @@ class MultiFormStep extends DataObject {
/** /**
* Get a validator specific to this form. * Get a validator specific to this form.
* The form is automatically validated in {@link Form->httpSubmission()}.
* *
* @return Validator * @return Validator
*/ */
@ -157,6 +158,24 @@ class MultiFormStep extends DataObject {
$this->write(); $this->write();
} }
/**
* Custom validation for a step. In most cases, it should be sufficient
* to have built-in validation through the {@link Validator} class
* on the {@link getValidator()} method.
* Use {@link Form->sessionMessage()} to feed back validation messages
* to the user. Please don't redirect from this method,
* this is taken care of in {@link next()}.
*
* @usedby next()
*
* @param array $data Request data
* @param Form $form
* @return boolean Validation success
*/
public function validateStep($data, $form) {
return true;
}
/** /**
* Returns the first value of $next_step * Returns the first value of $next_step
* *