Simple and lightweight standalone PHP library for validating data.
<?php
use SGP\Validation\Validator;
$input = [
'name' => 'Svilen Popov',
'comment' => 'This a comment',
'users' => [
[
'age' => 20,
'username' => 'username1'
],
[
'age' => 16,
'username' => 'username2'
]
]
];
$rules = [
'name' => 'required',
'comment' => 'required|min:20',
'users.*.age' => 'required|min:18'
];
$messages = [
'comment.min' => 'Your comment should have at least 20 chars.'
];
$validator = Validator::make($input, $rules, $messages);
$errors = $validator->errors()->all();
foreach ($errors as $error) {
echo $error . PHP_EOL;
}
and the above example will output:
Your comment should have at least 20 chars.
The users.1.age minimum is 18
Alpha | Integer |
---|---|
Alpha-Numeric | Max |
Boolean | Min |
Date after | Numeric |
Date before | Required |
Date |
Checks if the value contains only alphabetic characters.
'field' => 'alpha'
Checks if the valuecontains only alpha-numeric characters.
'field' => 'alpha_num'
Checks if the value is able to be cast as a boolean. Accepted input are true
, false
, 1
, 0
, "1"
, and "0"
.
'field' => 'boolean'
Checks if the value is a valid date and is after a given date.
'field' => 'date_after:2017-01-01'
Checks if the value is a valid date and is before a given date.
'field' => 'date_before:2017-01-01'
Checks if the value is a date/time string in the input format.
'field' => 'date'
Checks if the value is an integer.
'field' => 'int'
Checks if the value is less than given size. The rule works for strings, numerics and arrays.
'field' => 'max:20'
Checks if the value is greater than given size. The rule works for strings, numerics and arrays.
'field' => 'max:20'
Checks if the value is numeric.
'field' => 'numeric'
Checks if the value is not empty or null.
'field' => 'required'