Validator

class Validator

The Validator is used to chain Validators together and validate a set of data with it.

property availableValidators

protected

Holds the available validators.

createValidators($validators)

Creates instances of the available validators.

Parameters:
  • $validators (array) – the validators to load, key = name, value = classname within the namespace “ValdiValidator”
isValidRule($validator, $parameters, $value)

Validates a single rule.

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

boolean true if the value is valid

__construct()

Constructor.

addValidator($name, ValidatorInterface $validator)

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($rules, $value)

Validates a value via the given rules.

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

string[] the fields where the validation failed

isValid($rules, $data)

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’)