webiik / validator
The Validator validates data against user-defined rules.
1.1
2019-08-08 10:13 UTC
Requires
- php: >=7.2
This package is auto-updated.
Last update: 2025-02-08 22:27:42 UTC
README
Validator
The Validator validates data against user-defined rules.
Installation
composer require webiik/validator
Example
$validator = new \Webiik\Validator\Validator(); // Add input to validate and input validation rules $validator->addInput('meow', function () { return [ new \Webiik\Validator\Rules\StrLenMin(5, 'Err: Input is shorter than 5 chars.'), new \Webiik\Validator\Rules\StrLenMax(10, 'Err: Input is longer than 10 chars.'), ]; }, 'greeting'); // Validate all inputs and eventually get array of all invalid inputs $invalid = $validator->validate(); // Array ( [greeting] => Array ( [0] => Err: Input is shorter than 5 chars. ) )
Adding Inputs For Validation
addInput
addInput($input, callable $rules, string $name = '', bool $isRequired = false): void
addInput() adds an input for validation.
Parameters
- input input value
- rules callable that return an array of validation rule objects
- name optional input name. This name is used as input index in validate() result.
- isRequired optional. Indicates if input is required or not. Empty optional inputs are always considered as valid.
$validator->addInput('meow', function () { return [ new \Webiik\Validator\Rules\StrLenMin(5, 'Err: Input is shorter than 5 chars.'), new \Webiik\Validator\Rules\StrLenMax(10, 'Err: Input is longer than 10 chars.'), ]; }, 'greeting', true);
Validation Rules
Write Custom Validation Rule
You can write your custom validation rule. The only thing you have to do is to implement RuleInterface. Look at existing implementation of RuleInterface to get better insight.
Available Validation Rules
// Check if input is === $val Equal($val, string $errMsg = '')
// Check if input is >= $min and <= $max IntVal(int $min, int $max, string $errMsg = '')
// Check if input is <= $max IntValMax(int $max, string $errMsg = '')
// Check if input is >= $min IntValMin(int $max, string $errMsg = '')
// Check if input is email address isEmail(string $errMsg = '')
// Check if input is_float() isFloat(string $errMsg = '')
// Check if input is_int() isInt(string $errMsg = '')
// Check if input is_numeric() isNumeric(string $errMsg = '')
// Check if input is_object() isObject(string $errMsg = '')
// Check if input is not empty isPresent(string $errMsg = '')
// Check if input is_string() isString(string $errMsg = '')
// Check if input passes FILTER_VALIDATE_URL isUrl(string $errMsg = '')
// Check if input matches $regex regex(string $regex, string $errMsg = '')
// Check if input length is >= $min and <= $max StrLen(int $min, int $max, string $errMsg = '')
// Check if input length is >= $min StrLenMin(int $min, string $errMsg = '')
// Check if input length is <= $max StrLenMax(int $max, string $errMsg = '')
Validating Inputs
validate
validate($testAllRules = false): array
validate() validates all inputs and returns array of all invalid inputs sorted by input index.
Parameters
- testAllRules indicates if unfulfilled rule stops next rules checking
$invalid = $validator->validate();