This is a simple validator library to validate some input.
You can install it using composer :
composer require nicdamours/validator
To use the library, simply call the static method make
of the Validator object, then select your input to validate, and use the validate
function at the end.
Ex:
$myObject = [
'name' => 'John Doe',
'age' => 26,
'isAdult' => true,
'email' => 'j.doe@example.com',
'birthdate' => '1990-01-01'
];
return Validator::make($myObject)
->name('name')
->int('age')
->boolean('isAdult')
->email('email')
->date('birthDate')
->validate();
Be default, all parameters are optionals, unless you tell the validation function not to.
Ex :
$myObject = [
'name' => 'John Doe',
'age' => null,
'isAdult' => true,
'email' => 'j.doe@example.com',
'birthdate' => '1990-01-01'
];
return Validator::make($myObject)
->name('name', false) // this cannot be null.
->int('age')
->boolean('isAdult', false) // this cannot be null.
->email('email', false) // this cannot be null.
->date('birthDate', false) // this cannot be null.
->validate();
Currently, we support those validation method.
- password ( 7 to 20 letter, number, special char, must have one uppercase )
- title
- datetime (
YYYY-MM-DD hh:mm:ss
) - date (
YYYY-MM-DD
) - isoDatetime (
YYYY-MM-DDThh:mm:ss+0500
the timezone can be format0500
or05:00
, or any other number) - boolean ( as boolean or string )
- id ( int )
- int
- arrayOfString
- arrayOfInt
- path (
/var/www/html
) - version ( Ex: 1, 1.2, 1.2.3);
- HEX color (
#ffffff
,#fff
) - inArray ( if the element is in an array )