This is an easy to use and customisable PHP validator.
Note: This package is under development and not recommended for production.
Install via composer
composer require solbianca/validator
or add to composer.json
{
"require": {
"solbianca/php-validator": "1.*"
}
}
use SolBianca\Validator;
$validator = new Validator;
$validator->validate([
'age' => ['value' => 20, 'rules' => ['required', 'int']],
'name' => ['value' => 'John Doe', 'rules' => ['required']],
]);
if($validator->passed()) {
echo 'Validation passed!';
} else {
var_dump($validator->errors()->all();
}
Adding custom rules is simple. It can be any callable or object which implement SolBianca\Validator\Interfaces\RuleInterface
If the callable returns false, the rule fails.
$validator->addRule('sex', function ($value) {
return in_array($value, ['male', 'female']);
})->addRuleMessage('sex', 'Field `{field}` must be male or female. Given value `{value}`.');
$validator->validate([
'fruit' => ['value' => 'male', 'rules' => ['sex']],
]);
class SomeRule implements SolBianca\Validator\Interfaces\RuleInterface
{
// some code
}
// You can add as a string
$validator->addRule('sex', SomeRule::class);
// or as an object
$validator->addRule('sex', new SomeRule());
Validator have useful default rules as int
, required
and many more. You can rewrite any rule by your own.
$validator->addRule('int', function ($value) {
return (is_int($value)) && $value > 0);
})->addRuleMessage('sex', 'Field `{field}` must be integer ang greater than zero.');
$validator->validate([
'fruit' => ['value' => 'male', 'rules' => ['sex']],
]);
You can add custom error messages for any rule
$validator->addRuleMessage('required', 'You better fill in the {field} field, or else.');
$v->addRuleMessages([
'required' => 'You better fill in the {field} field, or else.',
'int' => 'The {field} needs to be an integer, but I found {value}.',
]);
Field Aliases helps you format any error messages without showing weird form names or the need to create a custom error.
$validator->validate([
'username_box' => ['value' => '', 'rules' => ['required'], 'alias' => 'Username']
]);
// Error output: "Field `Username` is required."
If the value is an array.
$validator->validate([
'some_input' => ['value' => [10, 20], 'rules' => ['array']],
]);
Checks if the value is within the intervals defined. This check is inclusive, so 5 is between 5 and 10.
$validator->validate([
'some_input' => ['value' => 5, 'rules' => ['between' => [5, 10]]],
]);
If the value is a boolean.
$validator->validate([
'some_input' => ['value' => true, 'rules' => ['bool']],
]);
If the value is a valid email.
$validator->validate([
'some_input' => ['value' => 'mail@example.com', 'rules' => ['email']],
]);
If the value is an integer, including numbers within strings. 1 and '1' are both classed as integers.
$validator->validate([
'some_input' => ['value' => 42, 'rules' => ['int']],
]);
If the value is a valid IP address.
$validator->validate([
'some_input' => ['value' => '127.0.0.1', 'rules' => ['ip']],
]);
Checks if one given input matches the other. For example, checking if password matches password_confirm.
$validator->validate([
'some_input' => ['value' => 1, 'rules' => ['int', 'matches' => 'other_input']],
'other_input' => ['value' => 1, 'rules' => ['int']]
]);
Check if string length is less than or equal to given int. To check the size of a number, pass the optional number option.
$validator->validate([
'some_input' => ['value' => 5, 'rules' => ['max' => 10]],
'other_input' => ['value' => 0.5, 'rules' => ['max' => [1.0, 'number']]],
]);
Check if string length is greater than or equal to given int. To check the size of a number, pass the optional number option.
$validator->validate([
'some_input' => ['value' => 5, 'rules' => ['min' => 1]],
'other_input' => ['value' => 0.5, 'rules' => ['min' => [0.0, 'number']]],
]);
If the value is a number, including numbers within strings.
Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal (e.g. 0xf4c3b00c), Binary (e.g. 0b10100111001), Octal (e.g. 0777) notation is allowed too but only without sign, decimal and exponential part.
$validator->validate([
'some_input' => ['value' => '5', 'rules' => ['number']],
]);
If the given input has a match for the regular expression given.
$validator->validate([
'some_input' => ['value' => 'bag', 'rules' => ['regex' => '/b[aeiou]g/']],
]);
If the value is present.
$validator->validate([
'some_input' => ['value' => true, 'rules' => ['required']],
]);
If the value is formatted as a valid URL.
$validator->validate([
'some_input' => ['value' => 'http://example.com', 'rules' => ['url']],
]);