This is my version of a simple php validator class.
composer require tyurderi/validator
$v = new Validator\Validator();
$v->addRule('unique_username', function($fields, $value, $params) {
// logic goes here
return true;
});
$v->add('username', 'tyurderi', 'required|min:3|max:30|unique_username', array(
'required' => 'The username is required.',
'min' => 'The username should be at least 3 characters long.',
'max' => 'The username should be at most 30 characters long.',
'unique_username' => 'The username is already in use.'
));
$v->validate();
if ($v->passes())
{
echo 'Validation was successfully!';
}
else
{
echo 'Validation failed.', PHP_EOL;
foreach ($v->errors() as $message)
{
echo $message, PHP_EOL;
}
}
The value must be not empty.
The value should equals 1, 2 or 3.
The value should be a valid email address.
The value should not equals 1, 2 or 3.
The value should be at least 3 characters long.
The value should be at most 30 characters long.
The value should be exact 30 characters long.
The value should equal or greater than 3.
The value should equal or smaller than 30.
The value should equal with the value of another registered field.
The value should equals the value at parameter 1. (someValue)
The value should contain at least 3 words.
MIT