I'm dynamically adding fields to a form, and validating that at least one of them has been completed.
However once the validation has completed, and redirectBack() has occurred, none of the posted fields have their values any more.
I've found a couple of posts (http://www.silverstripe.org/form-questions/show/11251?start=8 and http://www.silverstripe.org/form-questions/show/13495) which indicate that calling Session::set('FormInfo.Form_Name.data', $data); before return $this->redirectBack(); should resolve the problem, but it's not working. Perhaps this is for an older version, I'm running 3.1.3.
The complete code is as follows:
public function Cart() {
$tests = TestPage::get();
$fields = new FieldList(
TextField::create('Name')
);
foreach ($tests as $test) {
$fields->add(
NumericField::create('test' . $test->ID, $test->Title)
->setAttribute('type', 'number')
);
}
$validator = new RequiredFields(
'Name'
);
$actions = new FieldList(
new FormAction('Confirm', 'Purchase')
);
return new Form($this, 'Cart', $fields, $actions, $validator);
}
/**
* Validate the form and display order confirmation page
*
* @param $data
* @param $form
*
* @return SS_HTTPResponse
*/
public function Confirm($data, $form) {
// Error tracking flag
$error = false;
// Retrieve the tests
$tests = TestPage::get();
// Check if we have any qty for the tests
$qty = 0;
foreach ($tests as $test) {
$key = 'test' . $test->ID;
if (isset($data[$key]) && strlen($data[$key]) > 0) {
$qty += (int)$data[$key];
}
}
if ($qty < 1) {
$form->sessionMessage('You must select at least one test to purchase.', 'bad');
$error = true;
}
if ($error) {
// Repopulate the form
Session::set('FormInfo.' . $form->FormName() . '.data', $data);
// Redirect back
return $this->redirectBack();
}
return $this->redirect('Complete');
}