Ichi Validation is the fast and secure PHP validation library.
This package is Open Source According to MIT license
composer require jijihohococo/ichi-validation
your_project/vendor/jijihohococo/ichi-validation > php test/index.php
You can validate the input data with JiJiHoHoCoCo\IchiValidation\Validator.
For example, let's make the request which have 'name' , 'age' and 'email'.
We want to validate that 'name' is not null, 'age' must be integer and 'email' must be not null and avialable email format string
use JiJiHoHoCoCo\IchiValidation\Validator;
$validator=new Validator();
$boolResult=$validator->validate($_REQUEST,[
'name' => 'required' ,
'age' => 'required|integer' ,
'email' => ['required','email']
]);
// To get error messages in array if the boolResult is FALSE //
$errorMessages=$boolResult==FALSE ? $validator->getErrors() : [];
If you want to validate the data with multiple methods, you can separate methods by adding |
between the methods or putting those methods in the array.
While validating data, the system will add the error message to this data in the validator object if this data is not passed the validation according to the related method.
To validate the data is not null or not.
$validator->validate($_REQUEST,[
'name' => 'required'
]);
To validate the data is integer or not
$validator->validate($_REQUEST,[
'age' => 'integer'
]);
To validate the data is string or not
$validator->validate($_REQUEST,[
'phone' => 'string'
])
To validate the data is boolean or not
$validator->validate($_REQUEST,[
'married' => 'bool'
]);
To validate the data is double or not
$validator->validate($_REQUEST,[
'weight' => 'double'
]);
To validate the data is array or not
$validator->validate($_REQUEST,[
'highlights' => 'array'
]);
To validate the data is in the email format or not
$validator->validate($_REQUEST,[
'email' => 'email'
]);
To validate the $_FILES
's parameter is null or not
$validator->validate($_REQUEST,[
'image' => 'file'
]);
To validate the uploaded file is image or not
$validator->validate($_REQUEST,[
'image' => 'image'
]);
To validate the "field" is same as "confirm_field" or not
$validator->validate($_REQUEST,[
'password' => 'confirmed'
]);
This code is validating the "password" request is same as "confirm_password" or not
If the data is string, it is aimed to validate the number of this data string length is greater than the declared minimum number or not.
$validator->validate($_REQUEST,[
'name' => 'min:10'
]);
If the data is number, it is aimed to validate this number is greater than the declared minimum number or not.
$validator->validate($_REQUEST,[
'age' => 'min:18'
]);
If the data is uploaded file, it is aimed to validate the size of this uploaded file is greater than the declared minimum MB number or not.
$validator->validate($_REQUEST,[
'image' => 'min:3'
]);
If the data is string, it is aimed to validate the number of this data string length is less than the declared maximum number or not.
$validator->validate($_REQUEST,[
'name' => 'max:10'
]);
If the data is number, it is aimed to validate this number is less than the declared maximum number or not.
$validator->validate($_REQUEST,[
'age' => 'max:18'
]);
If the data is uploaded file, it is aimed to validate the size of this uploaded file is less than the declared maximum MB number or not.
$validator->validate($_REQUEST,[
'image' => 'max:3'
]);
To validate the data is exist in database's table or not
You must set PDO object firstly before using this method
$validator->setPDO($pdoObject);
And then you can use this method.
$validator->validate($_REQUEST,[
'email' => 'unique:user_table,email_field,NULL'
]);
Above code is validating request 'email' is same as any values of 'email_field' (column) of 'user_table' (table) or not.
Validating the data with this way is used to check the data duplication while inserting new data into database.
You can also make this way to validate the same process.
$validator->validate($_REQUEST,[
'email' => 'unique:user_table,email_field,'.NULL
]);
$validator->validate($_REQUEST,[
'email' => 'unique:user_table,email_field,'. 1
]);
Above code is validating request 'email' is same as value of 'email_field' (column) where the id is not 1 of 'user_table' (table) or not.
Validating the data with this way is used to check the data duplication while updating the data into database.
It is used where the primary key of the table is 'id'.
If the primary key of the table is not 'id', you must use the below code
$validator->validate($_REQUEST,[
'email' => 'unique:user_table,email_field,'. 1.',user_id'
]);
To validate the uploaded file's extension is one of the specific file extensions or not
$validator->validate($_REQUEST,[
'image' => 'mime:png,jpg,jpeg,gif'
]);
To validate the request number is between the specific two numbers or not
$validator->validate($_REQUEST,[
'age' => 'between:18,25'
]);
To validate the uploaded image is specific dimensions or not
There are sub-methods in this method.
width
min_width
max_width
height
min_height
max_height
To validate the uploaded image's width is delcared width or not
$validator->validate($_REQUEST,[
'image' => 'dimensions:width=100'
]);
To validate the uploaded image's width is greater than the declared minimum width or not
$validator->validate($_REQUEST,[
'image' => 'dimensions:min_width=100'
]);
To validate the uploaded image's width is less than the delcared maximum width or not
$validator->validate($_REQUEST,[
'image' => 'dimensions:max_width=100'
]);
To validate the uploaded image's height is delcared width or not
$validator->validate($_REQUEST,[
'image' => 'dimensions:height=100'
]);
To validate the uploaded image's height is greater than the declared minimum height or not
$validator->validate($_REQUEST,[
'image' => 'dimensions:min_height=100'
]);
To validate the uploaded image's height is less than the delcared maximum height or not
$validator->validate($_REQUEST,[
'image' => 'dimensions:max_height=100'
]);
You can validate your image's dimensions with multiple sub-methods
$validator->validate($_REQUEST,[
'image' => 'dimensions:width=100,height=100'
]);
To validate the uploaded image width and height is same as the declared ratio
$validator->validate($_REQUEST,[
'image' => 'image_ratio:1/3'
]);
You can customize the error message for validation
$validator->validate($_REQUEST,[
'name' => 'required',
'email' => ['required','string','email']
],[
'required' => 'Data is required'
]);
Above code is customizing the error message when the 'required' validation method is not passed.
$validator->validate($_REQUEST,[
'name' => 'required',
'email' => 'required|string|email'
],[
'name.required' => 'Name is required',
]);
Above code is customizing the error message when the 'required' validation method for 'name' request is not passed.
If you want to create your own validation method, you must create the validation class.
You can create your validation class via commandline.
Firstly you need to created the file named "ichi" under your project folder and use the below code in this file
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
use JiJiHoHoCoCo\IchiValidation\Command\ValidationCommand;
$validationCommand=new ValidationCommand;
$validationCommand->run(__DIR__,$argv);
And then you can create the validation class in your commandline
php ichi make:validation TestValidation
The default file folder is "app/Validations". So after making command, the validation class you created will be in the this default file folder. If you want to change the default folder path, you can change it in your "ichi" file.
$validationCommand=new ValidationCommand;
$validationCommand->setPath('new_app/Validations');
$validationCommand->run(__DIR__,$argv);
You must set the accepted validation rules in your created validation class. Let's make to accept only over age 21 in this created class to validate.
namespace App\Validations;
use JiJiHoHoCoCo\IchiValidation\CustomValidator;
class TestValidation extends CustomValidator{
public function __construct(){
}
public function rule(){
return $this->value>21;
}
public function showErrorMessage(){
return 'Your ' .$this->attribute . ' should be over 21.';
}
}
In calling your validation class
use App\Validations\TestValidation;
$validator=new Validator;
$validator->validate($_REQUEST,[
'name' => 'required|string',
'age' => ['required',new TestValidation()]
]);
$this->attribute
It is the validation data field name. For the example, it is 'age'.
$this->value
It is the data value. For the example, it is the value of 'age' request.
You can pass other values in the constructor.