PHP Class yii\validators\UniqueValidator

UniqueValidator checks if the value being validated is unique in the table column specified by the ActiveRecord class [[targetClass]] and the attribute [[targetAttribute]]. The following are examples of validation rules using this validator: php a1 needs to be unique ['a1', 'unique'] a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value ['a1', 'unique', 'targetAttribute' => 'a2'] a1 and a2 need to be unique together, and they both will receive error message [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']] a1 and a2 need to be unique together, only a1 will receive error message ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value) ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends Validator
Exibir arquivo Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$comboNotUnique string
$filter additional filter to be applied to the DB query used to check the uniqueness of the attribute value. This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] on the format of query condition), or an anonymous function with the signature function ($query), where $query is the [[\yii\db\Query|Query]] object that you can modify in the function.
$message the user-defined error message. When validating single attribute, 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 When validating mutliple attributes, it may contain the following placeholders: - {attributes}: the labels of the attributes being validated. - {values}: the values of the attributes being validated.
$targetAttribute the name of the ActiveRecord attribute that should be used to validate the uniqueness of the current attribute value. If not set, it will use the name of the attribute currently being validated. You may use an array to validate the uniqueness of multiple columns at the same time. The array values are the attributes that will be used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. If the key and the value are the same, you can just specify the value.
$targetClass the name of the ActiveRecord class that should be used to validate the uniqueness of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated.

Public Methods

Method Description
init ( )
validateAttribute ( $model, $attribute )

Private Methods

Method Description
addComboNotUniqueError ( Model $model, string $attribute ) Builds and adds [[comboNotUnique]] error message to the specified model attribute.

Method Details

init() public method

public init ( )

validateAttribute() public method

public validateAttribute ( $model, $attribute )

Property Details

$comboNotUnique public_oe property

Deprecation: since version 2.0.10, to be removed in 2.1. Use [[message]] property to setup custom message for multiple target attributes.
Since: 2.0.9
public string $comboNotUnique
return string

$filter public_oe property

additional filter to be applied to the DB query used to check the uniqueness of the attribute value. This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] on the format of query condition), or an anonymous function with the signature function ($query), where $query is the [[\yii\db\Query|Query]] object that you can modify in the function.
public $filter

$message public_oe property

the user-defined error message. When validating single attribute, 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 When validating mutliple attributes, it may contain the following placeholders: - {attributes}: the labels of the attributes being validated. - {values}: the values of the attributes being validated.
public $message

$targetAttribute public_oe property

the name of the ActiveRecord attribute that should be used to validate the uniqueness of the current attribute value. If not set, it will use the name of the attribute currently being validated. You may use an array to validate the uniqueness of multiple columns at the same time. The array values are the attributes that will be used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. If the key and the value are the same, you can just specify the value.
public $targetAttribute

$targetClass public_oe property

the name of the ActiveRecord class that should be used to validate the uniqueness of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated.
See also: targetAttribute
public $targetClass