Validation

PHPasap provides a very simple and extendable Validation library to validate any kind of data. Usually you would be using the validation library to validate the data received from a user submitted form.

Form validation library works on rules. You define a rule or a group of rule for a field. Form validation library check if the provided field satisfies the defined rule. If any error occurs then validation fails.

Quick Example

Below is an example usage of the Form Validation

$data = [
    "first_name" => "John",
    "last_name" => "Doe",
    "website" => "",
];

//Usually $data will be $_POST

$rules = [
    "first_name" => "required",
    "last_name" => "required",
    "website" => "required|url",
];

if( !Validator::validate($data, $rules) {
    //validation failed
    $error_array = Validator::errors();    
}
else {
    //validation successful
}

The validate() method accepts two paramters. First is the data (in array format) that has to be validated and the second is the array of rules to check the data against

If validation is successful then validate() returns boolean true. If any errors then it returns boolean false.

Adding rules

Rules array has to be an associative array. Each key in this array is the name of the field that has to be validated for.

So suppose the age field is suppose to be numeric. Then rule to be passed would be

"age" => "numeric"                           

Refer table below for the list of rules

Cascading multiple validation rules

To apply multiple rules to a field separate each rule by a pipe |. So say age is required and has to be numeric, then below is the rule to be defined for it.

"age" => "required|numeric"                           

Adding you own rules

Sometimes you may want to add your own custom rules to predefined ones. Like say username should be unique. Let's say we want to add our custom rule unique_username. First we'll add below rule to the rules array

"username" => "unique_username"                           

The validation library will first check if the unique_username exists in its predefined list of validation rules. If not then it will one by one do the following until it finds the callback function/method having this name

  • The optional third param to validate() method is an object. Validation library will check if this object has any method with the same name unique_username. If method exists it will call that method on the supplied object.
  • If third param is not set or if the object given in third param has no method with the name unique_username, then PHPasap will check if there exists a function unique_username(). If it exists, then it calls that function

While calling the callback method/function the validator will call with below two arguments and optional third argument

  • First param provided to the method, while calling, is the validator object itself.
  • Second param provided to the method, while calling, is the field value.
  • If instead of unique_username you provide unique_username:3 then validator will send value 3 as third param.

Both methods are explained below

Using Controller method as callback function

Remember the Controller method will be called only if the third param to validate method is the controller object. So below is how you would call the validate() method

$rules = [
    "first_name" => "required",
    "last_name" => "required",
    "website" => "required|url",
    "username" => "required|unique_username" //unique_username is our custom rule
];

//notice use of $this in third param
if( !Validator::validate($data, $rules, $this) {
    //validation failed
    $error_array = Validator::errors();    
}
else {
    //validation successful
}

Now in the same controller create below method

public function unique_username($validator_obj, $username_value) {
    if( DB::table("users")->where("username", "=", $username_value)->count() > 0 ) //username already in use
    $validator_obj->set_error("username", "Username already in use");
}

Using a function in any helper file as callback function

This option will be called if the third param of validate() method is not set. Else if set with an object, the object should not have a method with callback function name.

if( !Validator::validate($data, $rules) { //notice third param is missing here unlike above example
    //validation failed
    $error_array = Validator::errors();    
}
else {
    //validation successful
}

Now in any of the helpers file create the function unique_username. Remember all helpers file in app/helpers are autoloaded.

function unique_username($validator_obj, $username_value) {    
    if( DB::table("users")->where("username", "=", $username_value)->count() > 0 ) //username already in use
    $validator_obj->set_error("username", "Username already in use");
}

Few things to note here

  • Your validation function can use the set_error() method of Validator_Handler object, which is the first param passed, to set error message, if any error.
  • Second parameter passed to this function will be value of the field to validate

List of rules

Below is the list of predefined rules

Rule Description
required Checks if given parameter exists and is not empty()
set Checks if given parameter exists. Will validate true even if field value is 0 or null
email Checks if given parameter value is a valid email
float Checks if given parameter has float value
max Eg usage: max:5
Check if length of string is less than given integer
min Eg usage: min:5
Check if length of string is greater than given integer
natural Checks if given parameter has positive natural number i.e 0,1,2,3...
numeric Checks if given parameter has numeric value
url Checks if given parameter value is a valid url
bool Checks if given parameter value is boolean