webiik/validator

The Validator validates data against user-defined rules.

1.1 2019-08-08 10:13 UTC

This package is auto-updated.

Last update: 2024-05-08 20:45:58 UTC


README

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f77656269696b2f77656269696b2e737667 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e2e737667

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();

Resources