Form Validation Rules

When creating a form the fields described in Data types section are being used. No matter which method of form creation is used (from model / without model) fields have properties which are defined by default (it means the existence of these properties for these objects, thus specific values may not be defined ).

  • for all types of fields: "required", "must_match"
  • char: "regexp", "captcha", "length", "min_length", "max_length", "min_max_length", "unique"
  • int, float: "zero_allowed", "positive"
  • email, phone, url, redirect: "format", and also the properties inherited from "char"
  • text: the properties inherited from "char"
  • file, image: "allowed_extensions", "allowed_mime_types" (in the form of an array)

Based on these properties there is an initial validation of the form fields by "validate()" method.

//Specify the fields of a future form
$fields = array(array("Name", "char", "name", array("max_length" => 20)),
                array("Coupon code", "char", "cupon", array("length" => 8)),
                array("About", "text", "about"),
                array("Email", "email", "email", array("unique" => true)),
                array("Password", "password", "password", array("min_length" => 5)),
                array("Repeat a password", "password", "password-repeat", array("must_match" => "password")),
                array("Captcha", "char", "code", array("captcha" => "extra/captcha-simple/"))

//Create a form object
$form = new Form($fields);
//Specify mandatory fields 
$form -> setRequiredFields(array("name", "password", "password-repeat", "code"));
//Add one more field into form
$form -> addField(array("Photo", "image", "photo", array("files_folder" => "uploads")));
    $form -> getDataFromPost() -> validate();



This way upon fields validation the rules "max_length", "length", "required", "min_length", "must_match" for the appropriate fields will be activated. Based on specific situation, it is possible to redefine already specified (or existing, but empty) rules for the fields or to assign new ones.

Adding a new rule

To add a new rule you need to call "addRule()" method with an array of parameters in the following order:

  1. the name of a field (can also be an array or the "*" symbol for all fields)
  2. the name of property (related to data type this field belongs to)
  3. value of property for validation (if you pass a value as "->", then only the message will be changed and the rule remains in the previous way, it is convenient when changing an error message for the fields validated by regular expressions: int, float, email, phone, url, redirect).
  4. a message which is displayed if validation was not passed (if this parameter wasn't passed, a system message for this rule will be displayed instead).
//Make the field mandatory to fill in
$form -> addRule("cupon", "required", true, "Enter a coupon code for registration.");

//Make all fields mandatory to fill in
$form -> addRule("*", "required", true, "This field is to be filled in.");

//Add validation by regular expression
$form -> addRule("cupon", "regexp", "/^d+$/", "Coupon code may only contain digits.");

//Set the minimum length for the entered text
$form -> addRule("about", "min_length", 200, "For a story about yourself you need at least 200 symbols.");

//Set the max, length for several fields
$form -> addRule(array("name", "cupon", "email"), "max_length", 100, "Field length should not exceed 100 symbols.");

//Set a new message upon validation for unique e-mail address (instead of default message )
$form -> addRule("email", "unique", true, "This address is already in use by another user.");

//New message upon validation of correct e-mail address 
$form -> addRule("email", "format",  "/^[-a-z0-9_.]+@[-a-z0-9_.]+.[a-z]{2,5}$/i", "Enter a correct e-mail");
//New message (old format) upon validation of correct e-mail address
$form -> addRule("email", "format", "->", "Enter a correct e-mail ");

//Validation of repeat password value 
$form -> addRule("password-repeat", "must_match", "password", "Wrong repeat password");

//Limitation on types of uploaded files
$form -> addRule("photo", "allowed_extensions", array("gif", "jpg"), "Select  gif or  jpg");

Remove a rule

To delete a rule you need to pass 2 arguments into "removeRule()" method: the name of the field and the name of the rule that need to be deleted.

//Do not check the min. length 
$form -> removeRule("name", "max_length");
//Field is optional
$form -> removeRule("cupon", "required");
//Field is optional 
$form -> removeRule(array("name", "email"), "required");
//Remove validation of e-mail for unique address 
$form -> removeRule("email", "unique");
//Remove validation of all fields for max length 
$form -> removeRule("*", "max_length");

Additional info about formats of email, phone, url, redirect fields

The Field value of "url" type can't be simply numerical, and also consist of sequence of identical signs, different from alphabetic (for example "---" or "/-/") this is a mandatory rule no matter which format was specified. Below you can see the formats ("format" property) in the form of regular expressions, set by the system by default. They can be redefined by specifying appropriate properties using addRule method.

  • url - "/^[a-zd-]+$/"
  • redirect - "/^https?://(www.)?(([a-zd-]+.)+[a-z]{2,4}|localhost)((/|?).*)*$/"
  • email - "/^[-a-z0-9_.]+@[-a-z0-9_.]+.[a-z]{2,5}$/i"
  • phone - "/^[+ds()-]+$/"

Display Validation errors

//Entering values into form and validation
    $form -> getDataFromPost() -> validate();


//Display all errors as one list 
echo $form -> displayErrors();
//Display a form as a table, errors are shown near fields
echo $form -> setDisplayWithErrors() -> display();
//Display a form as blocks, errors are shown near fields 
echo $form -> setDisplayWithErrors() -> displayVertical();