1 | <?php |
||
42 | class UniqueValidator extends Validator |
||
43 | { |
||
44 | /** |
||
45 | * @var string the name of the ActiveRecord class that should be used to validate the uniqueness |
||
46 | * of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated. |
||
47 | * @see targetAttribute |
||
48 | */ |
||
49 | public $targetClass; |
||
50 | /** |
||
51 | * @var string|array the name of the [[\yii\db\ActiveRecord|ActiveRecord]] attribute that should be used to |
||
52 | * validate the uniqueness of the current attribute value. If not set, it will use the name |
||
53 | * of the attribute currently being validated. You may use an array to validate the uniqueness |
||
54 | * of multiple columns at the same time. The array values are the attributes that will be |
||
55 | * used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. |
||
56 | * If the key and the value are the same, you can just specify the value. |
||
57 | */ |
||
58 | public $targetAttribute; |
||
59 | /** |
||
60 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the uniqueness of the attribute value. |
||
61 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
62 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
63 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
64 | */ |
||
65 | public $filter; |
||
66 | /** |
||
67 | * @var string the user-defined error message. When validating single attribute, it may contain |
||
68 | * the following placeholders which will be replaced accordingly by the validator: |
||
69 | * |
||
70 | * - `{attribute}`: the label of the attribute being validated |
||
71 | * - `{value}`: the value of the attribute being validated |
||
72 | * |
||
73 | * When validating mutliple attributes, it may contain the following placeholders: |
||
74 | * |
||
75 | * - `{attributes}`: the labels of the attributes being validated. |
||
76 | * - `{values}`: the values of the attributes being validated. |
||
77 | * |
||
78 | */ |
||
79 | public $message; |
||
80 | /** |
||
81 | * @var string |
||
82 | * @since 2.0.9 |
||
83 | * @deprecated since version 2.0.10, to be removed in 2.1. Use [[message]] property |
||
84 | * to setup custom message for multiple target attributes. |
||
85 | */ |
||
86 | public $comboNotUnique; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | 48 | public function init() |
|
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | 39 | public function validateAttribute($model, $attribute) |
|
114 | { |
||
115 | /* @var $targetClass ActiveRecordInterface */ |
||
116 | 39 | $targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass; |
|
117 | 39 | $targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; |
|
118 | 39 | $conditions = $this->prepareConditions($targetAttribute, $model, $attribute); |
|
119 | |||
120 | 39 | foreach ($conditions as $value) { |
|
121 | 39 | if (is_array($value)) { |
|
122 | 6 | $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); |
|
123 | 6 | return; |
|
124 | } |
||
125 | 36 | } |
|
126 | |||
127 | 36 | if ($this->modelExists($targetClass, $conditions, $model)) { |
|
128 | 29 | if (count($targetAttribute) > 1) { |
|
129 | 6 | $this->addComboNotUniqueError($model, $attribute); |
|
130 | 6 | } else { |
|
131 | 29 | $this->addError($model, $attribute, $this->message); |
|
132 | } |
||
133 | 29 | } |
|
134 | 33 | } |
|
135 | |||
136 | /** |
||
137 | * Checks whether the $model exists in the database. |
||
138 | * |
||
139 | * @param string $targetClass the name of the ActiveRecord class that should be used to validate the uniqueness |
||
140 | * of the current attribute value. |
||
141 | * @param array $conditions conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
142 | * @param Model $model the data model to be validated |
||
143 | * |
||
144 | * @return bool whether the model already exists |
||
145 | */ |
||
146 | 36 | private function modelExists($targetClass, $conditions, $model) |
|
179 | |||
180 | /** |
||
181 | * Prepares a query by applying filtering conditions defined in $conditions method property |
||
182 | * and [[filter]] class property. |
||
183 | * |
||
184 | * @param ActiveRecordInterface $targetClass the name of the ActiveRecord class that should be used to validate |
||
185 | * the uniqueness of the current attribute value. |
||
186 | * @param array $conditions conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format |
||
187 | * |
||
188 | * @return ActiveQueryInterface|ActiveQuery |
||
189 | */ |
||
190 | 39 | private function prepareQuery($targetClass, $conditions) |
|
203 | |||
204 | /** |
||
205 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
206 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
207 | * |
||
208 | * @param string|array $targetAttribute the name of the [[\yii\db\ActiveRecord|ActiveRecord]] attribute that |
||
209 | * should be used to validate the uniqueness of the current attribute value. You may use an array to validate |
||
210 | * the uniqueness of multiple columns at the same time. The array values are the attributes that will be |
||
211 | * used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. |
||
212 | * If the key and the value are the same, you can just specify the value. |
||
213 | * @param Model $model the data model to be validated |
||
214 | * @param string $attribute the name of the attribute to be validated in the $model |
||
215 | |||
216 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
217 | */ |
||
218 | 42 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
231 | |||
232 | /** |
||
233 | * Builds and adds [[comboNotUnique]] error message to the specified model attribute. |
||
234 | * @param \yii\base\Model $model the data model. |
||
235 | * @param string $attribute the name of the attribute. |
||
236 | */ |
||
237 | 6 | private function addComboNotUniqueError($model, $attribute) |
|
255 | } |
||
256 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.