Form validation process
<? $fields = array(array("Organization", "char", "company", array("required" => true)), array("Number of employees", "int", "staff"), array("Business type", "enum", "type", array("values_list" => array("trade" => "Trade", "law" => "Law", "manufacturing" => "Manufacturing")), array("Email", "email", "email", array("required" => true)), array("Password to enter ", "password", "password", array("required" => true, "min_length" => 10, "letters_required" => true)), array("Activity description", "text", "about", array("required" => true)) ); $form = new Form($fields); ?>
Validation of all form fields
Before data validation you need to fill form field values, using "getDataFromArray()" or "getDataFromPost()" method. Fields of the form are validated by "validate()" method which reviews all the fields and checks them to meet requirements (mandatory fields, an integer, e-mail format etc). If during verification errors occurred, they will be stored in the object of the form to display them in template.
if(!empty($_POST)) { $form -> getDataFromPost() -> validate(); //Get fields values and their validation (all form fields) if(!$form -> isValid()) //If a form contains errors { ... } else { //No errors, do required actions //(creation / editing of record, send e-mail message etc) ... } } //Then in html part of a template display list of errors in a needed place echo $form -> displayErrors(); //Also we may display errors near fields echo $form -> setDisplayWithErrors() -> display();
Validation of selected form fields
Very often we have a situation when we only need to validate several fields of the form, for this case an array of field names will be passed for validation.
if(!empty($_POST)) { $form -> getDataFromPost() -> validate(array("company", "email")); }
Additional field validation and adding an error
$value = $form -> getValue("about"); //Or like this $value = $form -> about; if(strlen($value) < 100) $form -> addError("Please, enter a more detailed description of your activities."); //If form errors are shown near fields (after setDisplayWithErrors() call), //then we need to bind an additional error to the specified field, //by passing the name of field as a second parameter $form -> addError("Please, enter a more detailed description of your activities.", "about");
Set a field value individually
$form -> setValue("about", "Description of activites is not entered."); //Or like this $form -> about = "Description of activites is not entered.";
Previous section
Form validation rules