Form Validation : Using errors array.

Form Validation : Using errors array.

by Bhargava S -
Number of replies: 2

Hi,

This is a basic question but again after lot of search I could not get how to do.

I am for the first time trying to place custom validations. I have created errors array in validations method of the form. But I do not know where to capture this array.


In a page, I am creating the form as

        $cnClHpsFrm = new cl_manage_form( $CFG->wwwroot . '/local/hps/cl_manage_hps.php?id='.$currCourseid,array('housesArr'=>$housesArr,'currCrseStudentsArr'=>$currCrseStudentsArr,'currCourseid'=>$currCourseid) );

In this same file, I have the three conditions for form cancel, submit and else.

Please help and guide.




Average of ratings: -
In reply to Bhargava S

Re: Form Validation : Using errors array.

by Pinky Sharma -
Hi Bhargava,

You don't need to capture errors array to display error message during form validation. Moodle will automatically handle it internally .  You just need to use field name as a subscript of errors array for which you want to display error message.
For Ex -  If you want to check password should not be less than 5 character then the code inside validation function should be like that
if (!empty($data['password']) && strlen($data['password']) < 5) {
   $errors['password'] = " Password length should be more than 5";
}
This error message will automatically display above the password field if validation does not pass.
For more detail see : - https://docs.moodle.org/dev/lib/formslib.php_Validation
Hope this will help you.
In reply to Pinky Sharma

Re: Form Validation : Using errors array.

by Bhargava S -

Hi Pinky,

Thank you pointing out the subscript aspect. I missed that in documentation. Now I am also into moodle form validations smile