PHP Класс 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
С версии: 2.0
Автор: Qiang Xue ([email protected])
Наследование: extends yii\base\Component
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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'; }

Открытые методы

Метод Описание
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.

Защищенные методы

Метод Описание
validateValue ( mixed $value ) : array | null Validates a value.

Описание методов

addError() публичный Метод

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() публичный Метод

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
См. также: 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.
Результат string the client-side validation script. Null if the validator does not support client-side validation.

createValidator() публичный статический Метод

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.
Результат Validator the validator

init() публичный Метод

public init ( )

isActive() публичный Метод

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
Результат boolean whether the validator applies to the specified scenario.

isEmpty() публичный Метод

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
Результат boolean whether the value is empty

validate() публичный Метод

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.
Результат boolean whether the data is valid.

validateAttribute() публичный Метод

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() публичный Метод

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() защищенный Метод

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.
Результат array | null the error message and the parameters to be inserted into the error message. Null should be returned if the data is valid.

Описание свойств

$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.
public $attributes

$builtInValidators публичное статическое свойство

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

$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.
public $enableClientValidation

$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.
public $except

$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.
public $isEmpty

$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.
public $message

$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.
public $on

$skipOnEmpty публичное свойство

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

$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.
public $skipOnError

$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
public $when

$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'; }
См. также: when
public $whenClient