PHP Class yii\validators\Validator

Child classes should override the Validator::validateValue and/or Validator::validateAttribute methods to provide the actual logic of performing data validation. Child classes may also override Validator::clientValidateAttribute to provide client-side validation support. Validator declares a set of [[builtInValidators|built-in validators] which can be referenced using short names. They are listed as follows: - boolean: BooleanValidator - captcha: [[CaptchaValidator]] - compare: CompareValidator - date: DateValidator - default: DefaultValueValidator - double: NumberValidator - email: EmailValidator - exist: ExistValidator - file: FileValidator - filter: FilterValidator - image: ImageValidator - in: RangeValidator - integer: NumberValidator - match: RegularExpressionValidator - required: RequiredValidator - safe: SafeValidator - string: StringValidator - trim: FilterValidator - unique: UniqueValidator - url: UrlValidator
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Component
Mostrar archivo Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$attributes attributes to be validated by this validator. For multiple attributes, please specify them as an array; for single attribute, you may use either a string or an array.
$builtInValidators list of built-in validators (name => class or configuration)
$enableClientValidation whether to enable client-side validation for this validator. The actual client-side validation is done via the JavaScript code returned by Validator::clientValidateAttribute. If that method returns null, even if this property is true, no client-side validation will be done by this validator.
$except scenarios that the validator should not be applied to. For multiple scenarios, please specify them as an array; for single scenario, you may use either a string or an array.
$isEmpty a PHP callable that replaces the default implementation of Validator::isEmpty. If not set, Validator::isEmpty will be used to check if a value is empty. The signature of the callable should be function ($value) which returns a boolean indicating whether the value is empty.
$message the user-defined error message. It may contain the following placeholders which will be replaced accordingly by the validator: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated Note that some validators may introduce other properties for error messages used when specific validation conditions are not met. Please refer to individual class API documentation for details about these properties. By convention, this property represents the primary error message used when the most important validation condition is not met.
$on scenarios that the validator can be applied to. For multiple scenarios, please specify them as an array; for single scenario, you may use either a string or an array.
$skipOnEmpty whether this validation rule should be skipped if the attribute value is null or an empty string.
$skipOnError whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. Defaults to true.
$when a PHP callable whose return value determines whether this validator should be applied. The signature of the callable should be function ($model, $attribute), where $model and $attribute refer to the model and the attribute currently being validated. The callable should return a boolean value. This property is mainly provided to support conditional validation on the server-side. If this property is not set, this validator will be always applied on the server-side. The following example will enable the validator only when the country currently selected is USA: php function ($model) { return $model->country == Country::USA; }
$whenClient a JavaScript function name whose return value determines whether this validator should be applied on the client-side. The signature of the function should be function (attribute, value), where attribute is an object describing the attribute being validated (see Validator::clientValidateAttribute) and value the current value of the attribute. This property is mainly provided to support conditional validation on the client-side. If this property is not set, this validator will be always applied on the client-side. The following example will enable the validator only when the country currently selected is USA: javascript function (attribute, value) { return $('#country').val() === 'USA'; }

Public Methods

Method Description
addError ( Model $model, string $attribute, string $message, array $params = [] ) Adds an error about the specified attribute to the model object.
clientValidateAttribute ( Model $model, string $attribute, View $view ) : string Returns the JavaScript needed for performing client-side validation.
createValidator ( string | Closure $type, Model $model, array | string $attributes, array $params = [] ) : Validator Creates a validator object.
init ( )
isActive ( string $scenario ) : boolean Returns a value indicating whether the validator is active for the given scenario and attribute.
isEmpty ( mixed $value ) : boolean Checks if the given value is empty.
validate ( mixed $value, string &$error = null ) : boolean Validates a given value.
validateAttribute ( Model $model, string $attribute ) Validates a single attribute.
validateAttributes ( Model $model, array | null $attributes = null ) Validates the specified object.

Protected Methods

Method Description
validateValue ( mixed $value ) : array | null Validates a value.

Method Details

addError() public method

This is a helper method that performs message selection and internationalization.
public addError ( Model $model, string $attribute, string $message, array $params = [] )
$model yii\base\Model the data model being validated
$attribute string the attribute being validated
$message string the error message
$params array values for the placeholders in the error message

clientValidateAttribute() public method

You may override this method to return the JavaScript validation code if the validator can support client-side validation. The following JavaScript variables are predefined and can be used in the validation code: - attribute: an object describing the the attribute being validated. - value: the value being validated. - messages: an array used to hold the validation error messages for the attribute. - deferred: an array used to hold deferred objects for asynchronous validation - $form: a jQuery object containing the form element The attribute object contains the following properties: - id: a unique ID identifying the attribute (e.g. "loginform-username") in the form - name: attribute name or expression (e.g. "[0]content" for tabular input) - container: the jQuery selector of the container of the input field - input: the jQuery selector of the input field under the context of the form - error: the jQuery selector of the error tag under the context of the container - status: status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating
See also: yii\widgets\ActiveForm::enableClientValidation
public clientValidateAttribute ( Model $model, string $attribute, View $view ) : string
$model yii\base\Model the data model being validated
$attribute string the name of the attribute to be validated.
$view yii\web\View the view object that is going to be used to render views or view files containing a model form with this validator applied.
return string the client-side validation script. Null if the validator does not support client-side validation.

createValidator() public static method

Creates a validator object.
public static createValidator ( string | Closure $type, Model $model, array | string $attributes, array $params = [] ) : Validator
$type string | Closure the validator type. This can be either: * a built-in validator name listed in [[builtInValidators]]; * a method name of the model class; * an anonymous function; * a validator class name.
$model yii\base\Model the data model to be validated.
$attributes array | string list of attributes to be validated. This can be either an array of the attribute names or a string of comma-separated attribute names.
$params array initial values to be applied to the validator properties.
return Validator the validator

init() public method

public init ( )

isActive() public method

A validator is active if - the validator's on property is empty, or - the validator's on property contains the specified scenario
public isActive ( string $scenario ) : boolean
$scenario string scenario name
return boolean whether the validator applies to the specified scenario.

isEmpty() public method

A value is considered empty if it is null, an empty array, or an empty string. Note that this method is different from PHP empty(). It will return false when the value is 0.
public isEmpty ( mixed $value ) : boolean
$value mixed the value to be checked
return boolean whether the value is empty

validate() public method

You may use this method to validate a value out of the context of a data model.
public validate ( mixed $value, string &$error = null ) : boolean
$value mixed the data value to be validated.
$error string the error message to be returned, if the validation fails.
return boolean whether the data is valid.

validateAttribute() public method

Child classes must implement this method to provide the actual validation logic.
public validateAttribute ( Model $model, string $attribute )
$model yii\base\Model the data model to be validated
$attribute string the name of the attribute to be validated.

validateAttributes() public method

Validates the specified object.
public validateAttributes ( Model $model, array | null $attributes = null )
$model yii\base\Model the data model being validated
$attributes array | null the list of attributes to be validated. Note that if an attribute is not associated with the validator, or is is prefixed with `!` char - it will be ignored. If this parameter is null, every attribute listed in [[attributes]] will be validated.

validateValue() protected method

A validator class can implement this method to support data validation out of the context of a data model.
protected validateValue ( mixed $value ) : array | null
$value mixed the data value to be validated.
return array | null the error message and the parameters to be inserted into the error message. Null should be returned if the data is valid.

Property Details

$attributes public_oe property

attributes to be validated by this validator. For multiple attributes, please specify them as an array; for single attribute, you may use either a string or an array.
public $attributes

$builtInValidators public_oe static_oe property

list of built-in validators (name => class or configuration)
public static $builtInValidators

$enableClientValidation public_oe property

whether to enable client-side validation for this validator. The actual client-side validation is done via the JavaScript code returned by Validator::clientValidateAttribute. If that method returns null, even if this property is true, no client-side validation will be done by this validator.
public $enableClientValidation

$except public_oe property

scenarios that the validator should not be applied to. For multiple scenarios, please specify them as an array; for single scenario, you may use either a string or an array.
public $except

$isEmpty public_oe property

a PHP callable that replaces the default implementation of Validator::isEmpty. If not set, Validator::isEmpty will be used to check if a value is empty. The signature of the callable should be function ($value) which returns a boolean indicating whether the value is empty.
public $isEmpty

$message public_oe property

the user-defined error message. It may contain the following placeholders which will be replaced accordingly by the validator: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated Note that some validators may introduce other properties for error messages used when specific validation conditions are not met. Please refer to individual class API documentation for details about these properties. By convention, this property represents the primary error message used when the most important validation condition is not met.
public $message

$on public_oe property

scenarios that the validator can be applied to. For multiple scenarios, please specify them as an array; for single scenario, you may use either a string or an array.
public $on

$skipOnEmpty public_oe property

whether this validation rule should be skipped if the attribute value is null or an empty string.
public $skipOnEmpty

$skipOnError public_oe property

whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. Defaults to true.
public $skipOnError

$when public_oe property

a PHP callable whose return value determines whether this validator should be applied. The signature of the callable should be function ($model, $attribute), where $model and $attribute refer to the model and the attribute currently being validated. The callable should return a boolean value. This property is mainly provided to support conditional validation on the server-side. If this property is not set, this validator will be always applied on the server-side. The following example will enable the validator only when the country currently selected is USA: php function ($model) { return $model->country == Country::USA; }
See also: whenClient
public $when

$whenClient public_oe property

a JavaScript function name whose return value determines whether this validator should be applied on the client-side. The signature of the function should be function (attribute, value), where attribute is an object describing the attribute being validated (see Validator::clientValidateAttribute) and value the current value of the attribute. This property is mainly provided to support conditional validation on the client-side. If this property is not set, this validator will be always applied on the client-side. The following example will enable the validator only when the country currently selected is USA: javascript function (attribute, value) { return $('#country').val() === 'USA'; }
See also: when
public $whenClient