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.

Form Questions /

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


Reply


3 Posts   1124 Views

Avatar
prawnstar

Community Member, 127 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 (http://silverstripe.org/form-questions/show/10686), but that doesn't seem to fix the issue either.

Action:
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.

/mysite/code/LandingFormPage.php:

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();
      $form->saveInto($submission);
      if(DataObject::get_one('LandingFormSubmission', "Email = '".$submission->getField('Email')."'")) {
         $form->addErrorMessage("Email", 'That email address has already been used to register.', "bad");
         // Session::set("FormInfo.LandingForm_LandingForm.data", $data);
         return Director::redirectBack();
      } else {
         $submission->write();
         Director::redirectBack();
      }
   }

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.

Chris.b

Avatar
lx

Community Member, 83 Posts

19 March 2011 at 10:05pm

Hi prawnstar,

take a look at this: http://silverstripe.org/all-other-modules/show/16073

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.

bye
lx

Avatar
prawnstar

Community Member, 127 Posts

20 March 2011 at 5:55am

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

Chris.b