Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » (solved)How to pass data back to a form after submission error?

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

Page: 1
Go to End
Author Topic: 1081 Views
  • prawnstar
    Avatar
    Community Member
    116 Posts

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

    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

  • lx
    Avatar
    Community Member
    82 Posts

    Re: (solved)How to pass data back to a form after submission error? Link to this post

    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

  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: (solved)How to pass data back to a form after submission error? Link to this post

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

    Chris.b

    1081 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.