Valdi\Validator

class Valdi\Validator
The Validator is used to chain Validators together and validate a set of data with it.
property $availableValidators

Holds the available validators.

createValidators()

Creates instances of the available validators.

Parameters:
  • $validators (array) – the validators to load, key = name, value = classname within the namespace “\Valdi\Validator”
isValidRule()

Validates a single rule.

Parameters:
  • $validator (string) – the validator to use
  • $parameters (string[]) – the validation parameters, depending on the validator
  • $value (null|string) – the value to validate
Returns:

boolean $ true if the value is valid

Throws:

ValidatorException $ thrown if the validator is not available

__construct()

Constructor.

addValidator()

Adds additional validator. It can override existing validators as well.

Parameters:
  • $name (string) – the name of the new validator.
  • $validator (ValidatorInterface) – the validator to add
isValidValue()

Validates a value via the given rules.

Parameters:
  • $rules (array) – the validation rules
  • $value (null|string) – the value to validate
Returns:

string[] $ the fields where the validation failed

Throws:

ValidatorException $ thrown if a validator is not available

isValid()

Performs the actual validation.

Parameters:
  • $rules (array) – the validation rules: an array with a field name as key and an array of rules to use for this field; each rule is an array with the validator name as first element and parameters as following elements; example: array(‘a’ => array(array(‘required’)), ‘b’ => array(array(‘min’, 1)))
  • $data (array) – the data to validate as a map
Returns:

array<string,boolean|array> $ the validation result having the keys “valid” (true or false) and the key “errors” containing all failed fields as keys with arrays of the failed validator names; example where the field “b” from the above sample failed due to the min validator: array(‘valid’ => false, errors => array(‘b’ => array(‘min’))) the “or” validator doesn’t return a single string on validation error; instead, it returns an array listing all failed validators of it: array(‘or’ => array(‘url’, ‘email’)

Throws:

ValidatorException $ thrown if a validator is not available