lithium\util\Validator::check()

public static method

Checks a set of values against a specified rules list. This method may be used to validate any arbitrary array of data against a set of validation rules.

Parameters

  • array $values

    An array of key/value pairs, where the values are to be checked.

  • array $rules

    An array of rules to check the values in $values against. Each key in $rules should match a key contained in $values, and each value should be a validation rule in one of the allowable formats. For example, if you are validating a data set containing a 'credit_card' key, possible values for $rules would be as follows:

    • array('credit_card' => 'You must include a credit card number'): This is the simplest form of validation rule, in which the value is simply a message to display if the rule fails. Using this format, all other validation settings inherit from the defaults, including the validation rule itself, which only checks to see that the corresponding key in $values is present and contains a value that is not empty. Please note when globalizing validation messages: When specifying messages, it may be preferable to use a code string (i.e. 'ERR_NO_TITLE') instead of the full text of the validation error. These code strings may then be translated by the appropriate tools in the templating layer.
    • array('credit_card' => ['creditCard', 'message' => 'Invalid CC #']): In the second format, the validation rule (in this case creditCard) and associated configuration are specified as an array, where the rule to use is the first value in the array (no key), and additional settings are specified as other keys in the array. Please see the list below for more information on allowed keys.
    • The final format allows you to apply multiple validation rules to a single value, and it is specified as follows: array('credit_card' => [ ['notEmpty', 'message' => 'You must include credit card number'], ['creditCard', 'message' => 'Your credit card number must be valid'] ]);
  • array $options

    Validator-specific options. Each rule defined as an array can contain any of the following settings (in addition to the first value, which represents the rule to be used):

    • 'message' string: The error message to be returned if the validation rule fails. See the note above regarding globalization of error messages.
    • 'required' boolean: Represents whether the value is required to be present in $values. If 'required' is set to false, the validation rule will be skipped if the corresponding key is not present. Defaults to true.
    • 'skipEmpty' boolean: Similar to 'required', this setting (if true) will cause the validation rule to be skipped if the corresponding value is empty (an empty string or null). Defaults to false.
    • 'format' string: If the validation rule has multiple format definitions (see the add() or init() methods), the name of the format to be used can be specified here. Additionally, two special values can be used: either 'any', which means that all formats will be checked and the rule will pass if any format passes, or 'all', which requires all formats to pass in order for the rule check to succeed.

Returns

array

Returns an array containing all validation failures for data in $values, where each key matches a key in $values, and each value is an array of that element's validation errors.

Filter

This method can be filtered.

Source

	public static function check(array $values, array $rules, array $options = []) {
		$defaults = [
			'notEmpty',
			'message' => null,
			'required' => true,
			'skipEmpty' => false,
			'format' => 'any',
			'on' => null,
			'last' => false
		];

		$options += $defaults;
		$params = compact('values', 'rules', 'options');

		return Filters::run(get_called_class(), __FUNCTION__, $params, function($params) {
			$values = $params['values'];
			$rules = $params['rules'];
			$options = $params['options'];

			$errors = [];
			$events = (array) (isset($options['events']) ? $options['events'] : null);
			$values = array_merge($values, Set::flatten($values));

			foreach ($rules as $field => $rules) {
				$rules = is_string($rules) ? ['message' => $rules] : $rules;
				$rules = is_array(current($rules)) ? $rules : [$rules];
				$errors[$field] = [];
				$options['field'] = $field;

				foreach ($rules as $key => $rule) {
					if (array_key_exists('required', $rule) && $rule['required'] === null) {
						unset($rule['required']);
					}
					$rule += $options + compact('values');
					list($name) = $rule;

					if ($events && $rule['on'] && !array_intersect($events, (array) $rule['on'])) {
						continue;
					}
					if (!array_key_exists($field, $values)) {
						if ($rule['required']) {
							$errors[$field][$key] = $rule['message'] ?: $key;
						}
						if ($rule['last']) {
							break;
						}
						continue;
					}
					if (empty($values[$field]) && $rule['skipEmpty']) {
						continue;
					}

					if (!static::rule($name, $values[$field], $rule['format'], $rule + $options)) {
						$errors[$field][$key] = $rule['message'] ?: $key;

						if ($rule['last']) {
							break;
						}
					}
				}
			}
			return array_filter($errors);
		});
	}