Validator is a server side data validation library for PHP. Validate html form-data, objects, arrays and json etc.

 

Validator

Validator Logo

Validator is a server side data validation library for PHP. Validate html form-data, objects, arrays and json etc. Validator make data validation simple.

Installation

  • Install composer if you have not installed.
composer require unicframework/validator

Set Validation Rules

We can set data validation rules using rules method.

use Validator\Validator;

// Set data validation rules
$validator = Validator::make([
  'first_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'last_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'email' => [
    'required' => true,
    'not_null' => true,
    'email' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'in' => ['male', 'female']
  ],
  'password' => [
    'required' => true,
    'not_null' => true,
    'minlength' => 6
  ]
]);

We can also use a shorthand method to set data validation rules, which is very simple and shorter.

use Validator\Validator;

// Set data validation rules
$validator = Validator::make([
  'first_name,last_name' => 'required|not_null|string',
  'email' => 'required|not_null|email',
  'gender' => 'required|not_null|in:male,female',
  'password' => 'required|not_null|minlength:6'
]);

Set Error Messages

We can set error messages using messages method. if we don't set error messages then validator automatically generate error messages for you.

use Validator\Validator;

// Set validation error messages
$validator = Validator::make([
  'first_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'last_name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'email' => [
    'required' => true,
    'not_null' => true,
    'email' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'in' => ['male', 'female']
  ],
  'password' => [
    'required' => true,
    'not_null' => true,
    'minlength' => 6
  ]
],
[
  'first_name' => [
    'required' => 'First name is required',
    'not_null' => 'First name can not be null',
    'string' => 'First name should be in string'
  ],
  'last_name' => [
    'required' => 'Last name is required',
    'not_null' => 'Last name can not be null',
    'string' => 'Last name should be in string'
  ],
  'email' => [
    'required' => 'Email is required',
    'not_null' => 'Email can not be null',
    'email' => 'Please enter valid email address'
  ],
  'gender' => [
    'required' => 'Gender is required',
    'not_null' => 'Gender can not be null',
    'in' => 'Please select valid gender'
  ],
  'password' => [
    'required' => 'Password is required',
    'not_null' => 'Password can not be null',
    'minlength' => 'Password length should be minimum 5 characters'
  ]
]);

We can also use a shorthand method to set data validation rules, which is very simple and shorter.

use Validator\Validator;

// Set validation error messages
$validator = Validator::make([
  'first_name,last_name' => 'required|not_null|string',
  'email' => 'required|not_null|email',
  'gender' => 'required|not_null|in:male,female',
  'password' => 'required|not_null|minlength:6'
],
[
  'first_name,last_name' => 'required:Name is required|not_null:Name can not be null|string:Name should be in string',
  'email' => 'required:Email is required|not_null:Email can not be null|email:Please enter valid email address',
  'gender' => 'required:Gender is required|not_null:Gender can not be null|in:Please select valid gender'
  'password' => 'required: Password is required|not_null:Password can not be null|minlength:Password length should be minimum 5 characters',
]);

Validate Data

Using validator we can validate html form-data, array, object and json data. Validator validate data according to rules. It will return true if all the data are valid, otherwise it will return false.

Validate single data :

use Validator\Validator;

$validator = Validator::make([
  'name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'string' => true,
    'lowercase' => true,
    'in' => ['male', 'female', 'other']
  ],
  'contact.email' => [
    'required' => true,
    'not_null' => true,
    'email' => true,
  ]
],
[
  'contact.email' => [
    'required' => 'Please enter email address.',
    'email' => 'Please enter valid email address.'
  ]
]);

// Data for validation
// We can validate any data like arrays, objects, and json etc.
$data = [
  'name' => 'abc xyz',
  'gender' => 'male',
  'contact' => [
    'email' => 'abc@gmail.com'
  ]
];

// Validate data
if($validator->validate($data)) {
  //Ok data is valid
} else {
  // Display validation errors
  print_r($validator->errors();
}

Validate multiple sets of data :

use Validator\Validator;

$validator = Validator::make([
  'name' => [
    'required' => true,
    'not_null' => true,
    'string' => true
  ],
  'gender' => [
    'required' => true,
    'not_null' => true,
    'string' => true,
    'lowercase' => true,
    'in' => ['male', 'female', 'other']
  ],
  'contact.email' => [
    'required' => true,
    'not_null' => true,
    'email' => true,
  ]
],
[
  'contact.email' => [
    'required' => 'Please enter email address.',
    'email' => 'Please enter valid email address.'
  ]
]);

// Data for validation
// We can validate any data like arrays, objects, and json etc.
$data = [
  [
    'name' => 'abc xyz',
    'gender' => 'male',
    'contact' => [
      'email' => 'xyz@gmail.com'
    ]
  ],
  [
    'name' => 'xyz abc',
    'gender' => 'male',
    'contact' => [
      'email' => 'xyz@gmail.com'
    ]
  ]
];

// Validate multiple sets of data
if($validator->validate($data, true)) {
  // Ok data is valid
} else {
  // Display validation errors
  print_r($validator->errors());
}

Get Invalid Errors

We can get errors using errors method. the errors method return an array of errors.

// Get all errors
$errors = $validator->errors();

Comments