NEW introduce getValueFromOtherStep() and copyValueFromOtherStep() (Fixes #55)

This commit is contained in:
Peter Thaleikis 2016-03-04 15:34:32 +13:00 committed by Will Rossiter
parent 75f304993d
commit 0ee0355e26
2 changed files with 96 additions and 2 deletions

View File

@ -333,7 +333,49 @@ This new method you create would then become available in the progress indicator
template. template.
### 7. Finishing it up ### 7. Loading values from other steps
There are several use cases were you want to pre-populate a value based
based on the submission value of another step. There are two methods supporting this:
* `getValueFromOtherStep()` load any submitted value from another step from the session
* `copyValueFromOtherStep()` saves you the repeated work of adding the same lines of code again and again.
Here is an example of how to populate the email address from step 1 in step2 :
:::php
<?php
class Step1 extends MultiFormStep
{
public static $next_steps = 'Step2';
public function getFields() {
return new FieldList(
new EmailField('Email', 'Your email')
);
}
}
class Step2 extends MultiFormStep
{
public static $next_steps = 'Step3';
public function getFields() {
$fields = new FieldList(
new EmailField('Email', 'E-mail'),
new EmailField('Email2', 'Verify E-Mail')
);
// set the email field to the input from Step 1
$this->copyValueFromOtherStep($fields, 'Step1', 'Email');
return $fields;
}
}
### 8. Finishing it up
Now that we've got a structure set up to collect form data along each step, and Now that we've got a structure set up to collect form data along each step, and
progress through successfully, we need to customise what happens at the end of progress through successfully, we need to customise what happens at the end of
@ -398,7 +440,7 @@ Here is an example of what we could do here:
} }
#### 8. Organisation data model #### 9. Organisation data model
The class Organisation is mentioned in the above example but doesn't exist at The class Organisation is mentioned in the above example but doesn't exist at
the moment (unlike the existing Member() class which looks after the member the moment (unlike the existing Member() class which looks after the member

View File

@ -79,6 +79,13 @@ class MultiFormStep extends DataObject {
*/ */
protected $extraClasses = array(); protected $extraClasses = array();
/**
* Temporary cache to increase the performance for repeated look ups.
*
* @var array $cache
*/
protected $step_data_cache = array();
/** /**
* Form fields to be rendered with this step. * Form fields to be rendered with this step.
* (Form object is created in {@link MultiForm}. * (Form object is created in {@link MultiForm}.
@ -410,4 +417,49 @@ class MultiFormStep extends DataObject {
public function getExtraClasses() { public function getExtraClasses() {
return join(' ', array_keys($this->extraClasses)); return join(' ', array_keys($this->extraClasses));
} }
/**
* Returns the submitted value, if any, of any steps.
*
* @param string $fromStep (classname)
* @param string $key
*
* @return mixed
*/
public function getValueFromOtherStep($fromStep, $key) {
// load the steps in the cache, if this one doesn't exist
if (!array_key_exists('steps_' . $fromStep, $this->step_data_cache)) {
$steps = MultiFormStep::get()->filter('SessionID', $this->form->session->ID);
if($steps) {
foreach($steps as $step) {
$this->step_data_cache['steps_' . $step->ClassName] = $step->loadData();
}
}
}
// check both as PHP isn't recursive
if(isset($this->step_data_cache['steps_' . $fromStep])) {
if(isset($this->step_data_cache['steps_' . $fromStep][$key])) {
return $this->step_data_cache['steps_' . $fromStep][$key];
}
}
return null;
}
/**
* allows to get a value from another step copied over
*
* @param FieldList $fields
* @param string $formStep
* @param string $fieldName
* @param string $fieldNameTarget (optional)
*/
public function copyValueFromOtherStep(FieldList $fields, $formStep, $fieldName, $fieldNameTarget = null) {
// if a target field isn't defined use the same fieldname
if (!$fieldNameTarget) $fieldNameTarget = $fieldName;
$fields->fieldByName($fieldNameTarget)->setValue($this->getValueFromOtherStep($formStep, $fieldName));
}
} }