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

Custom validation of the form fields on the server side


Reply

12 Posts   5511 Views

Avatar
lx

14 March 2013 at 3:44am Community Member, 83 Posts

Benwu, did you check out the netefxvalidator module ?

Avatar
BenWu

14 March 2013 at 4:05am (Last edited: 14 March 2013 4:05am), Community Member, 90 Posts

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.

Avatar
JonoM

3 April 2013 at 7:09am (Last edited: 3 April 2013 7:10am), Community Member, 103 Posts

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 [url=http://www.ssbits.com/tutorials/2010/site-members-registering-users-and-allowing-them-to-edit-their-details/]SSBits post[/url].

//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();
   }
   
   ...

Avatar
martimiz

3 April 2013 at 9:44am Forum Moderator, 1095 Posts

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