Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

(solved)How to pass data back to a form after submission error?

Go to End

3 Posts   1456 Views


Community Member, 130 Posts

17 March 2011 at 10:26am

Edited: 17/03/2011 10:27am

I know this has been covered elsewhere, but I'm not seeing a solution that works for me. This is the closest (, but that doesn't seem to fix the issue either.

When a user submits the form, after $validator/capthca and before data is written to the database, the script checks to make sure there isn't a duplicate email already in the database.

Expected result:
If a duplicate email is found, the user is returned to the form with an error message AND their form data re-populated in the form fields.

Actual result:
If a duplicate record is found the user is returned to the form, an error message tells them a duplicate record was found, but none of the form fields are re-populated with the user's input.

Note: Otherwise this works fine. Fields validate and new users are added to the database.


class LandingFormPage_Controller extends Page_Controller {

	function LandingForm() {
		$fields = new FieldSet(
			new TextField('FirstName','First Name*'),
			new TextField('LastName','Last Name*'),
			new EmailField('Email','Email*'),
			new TextField('Company','Company'),
			new TextField('Title','Title'),
			new TextField('Phone','Phone') )
		$actions = new FieldSet(
			new FormAction('doSignup', 'Sign Up')
		$validator = new RequiredFields('FirstName', 'LastName', 'Email');
		$form = new Form($this, 'LandingForm', $fields, $actions, $validator);
		SpamProtectorManager::update_form($form,'','','Help Us Stop Spam By Verifying These Letters');
		return $form;

	function doSignup($data, $form) {
		$submission = new LandingFormSubmission();
		if(DataObject::get_one('LandingFormSubmission', "Email = '".$submission->getField('Email')."'")) {
			$form->addErrorMessage("Email", 'That email address has already been used to register.', "bad");
			// Session::set("", $data); 
			return Director::redirectBack();
		} else {

As you can see I've also tried setting my data, but that doesn't seem to make a difference. A nudge in the right direction would be greatly appreciated.



Community Member, 83 Posts

19 March 2011 at 10:05pm

Hi prawnstar,

take a look at this:

1. Download the module "NetefxValidator".
2. Define a rule for the emailadress like:

$rule_Email_unique = new NetefxValidatorRule ("EMail", "UNIQUE", array('EMail','LandingFormSubmission'), "That email address has already been used to register");

3. Define all your other rules, like all required fields.

$rule_FirstName_Required = new NetefxValidatorRule("FirstName", "REQUIRED", "", "Please enter your first name");

4. Add the validation rules to your form (replaces your validator)

$validator = new NetefxValidator($rule_FirstName_Required, $rule_Email_unique);

5. get rid of all validation in doSignup()

This is the good news about the validator module.
You can make all kinds of validation, described as rules.
So when doSignup is executed , you can be shure your form is filled correctly.

for any further questions about the module, please use its form thread.



Community Member, 130 Posts

20 March 2011 at 5:55am

Wow! Thanks for that. That is exactly the behavior I was looking for.