Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Custom validation of the form fields on the server side

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

Page: 1 2
Go to End
Author Topic: 5170 Views
  • lx
    Avatar
    Community Member
    82 Posts

    Re: Custom validation of the form fields on the server side Link to this post

    Benwu, did you check out the netefxvalidator module ?

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: Custom validation of the form fields on the server side Link to this post

    netefxvalidator is quite handy.

    however, i think the best solution is to validate dats just before the write method. if fails, do a redirectBack. It's a shame the form got clear out. Hope wilr got time to find out? not sure how data are passed from one page to another.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Custom validation of the form fields on the server side Link to this post

    I've always thought there had to be an easy way to do this - took a lot of frustrating digging to work out a solution today but finally found one thanks to a hint in an SSBits post.

    //The function that handles our form submission
    function sendForm($data, $form) {
       
       //Custom validation
       if ($data['FieldName'] == 0) { //Some logical test
          //Add error message to field
          $form->addErrorMessage('FieldName','Something went wrong!','required');
          //Set form data from submitted values
          Session::set("FormInfo.".$form->FormName().".data", $data);
          //Return back to form
          return $this->redirectBack();
       }
       
       ...

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Custom validation of the form fields on the server side Link to this post

    Just something I stumbled upon today in 2.4: instead of using RequiredFields you can use the CustomRequiredFields class that for every fiield lets you add an array, where you can add functionality to check if the value for that field is correct, and the error message to show if it isn't.

    The message will be displayed next to the field, just like the regular required field message. I used that today in a form with a double e-mailfield, where the values in both fields should be equal. Works like a charm! Unfortunately, unless I'm mistaken, it doesn't seem to have made it into 3.0...

    5170 Views
Page: 1 2
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.