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


Go to End
Reply


12 Posts   5868 Views

Avatar
lx

Community Member, 83 Posts

14 March 2013 at 3:44am

Benwu, did you check out the netefxvalidator module ?

Avatar
BenWu

Community Member, 91 Posts

14 March 2013 at 4:05am

Edited: 14/03/2013 4:05am

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

Community Member, 103 Posts

3 April 2013 at 7:09am

Edited: 03/04/2013 7:10am

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

Forum Moderator, 1132 Posts

3 April 2013 at 9:44am

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

Go to Top