Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » [SOLVED] Repopulate fields after custom validation

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

Page: 1
Go to End
Author Topic: 394 Views
  • mattdwen
    Avatar
    Community Member
    12 Posts

    [SOLVED] Repopulate fields after custom validation Link to this post

    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');
          }

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: [SOLVED] Repopulate fields after custom validation Link to this post

    Hi,

    If I recall correctly, you actually need to set a form error. If you don't want to set an error on a specific field, you can trigger the behaviour anyway by calling this before you redirect back:

    Session::set("FormInfo.{$form->FormName()}.errors", array());

    The reason is that the data from the session isn't loaded unless an error occurs.

    Loz

  • mattdwen
    Avatar
    Community Member
    12 Posts

    Re: [SOLVED] Repopulate fields after custom validation Link to this post

    Thanks Loz, that works as advertised.

    394 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.