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 | */ |
||
57 | public $targetAttribute; |
||
58 | /** |
||
59 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the uniqueness of the attribute value. |
||
60 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
61 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
62 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
63 | */ |
||
64 | public $filter; |
||
65 | /** |
||
66 | * @var string the user-defined error message. |
||
67 | * |
||
68 | * When validating single attribute, it may contain |
||
69 | * the following placeholders which will be replaced accordingly by the validator: |
||
70 | * |
||
71 | * - `{attribute}`: the label of the attribute being validated |
||
72 | * - `{value}`: the value of the attribute being validated |
||
73 | * |
||
74 | * When validating mutliple attributes, it may contain the following placeholders: |
||
75 | * |
||
76 | * - `{attributes}`: the labels of the attributes being validated. |
||
77 | * - `{values}`: the values of the attributes being validated. |
||
78 | * |
||
79 | */ |
||
80 | public $message; |
||
81 | /** |
||
82 | * @var string |
||
83 | * @since 2.0.9 |
||
84 | * @deprecated since version 2.0.10, to be removed in 2.1. Use [[message]] property |
||
85 | * to setup custom message for multiple target attributes. |
||
86 | */ |
||
87 | public $comboNotUnique; |
||
88 | /** |
||
89 | * @var string and|or define how target attributes are related |
||
90 | * @since 2.0.11 |
||
91 | */ |
||
92 | public $targetAttributeJunction = 'and'; |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | 57 | public function init() |
|
99 | { |
||
100 | 57 | parent::init(); |
|
101 | 57 | if ($this->message !== null) { |
|
102 | 3 | return; |
|
103 | } |
||
104 | 57 | if (is_array($this->targetAttribute) && count($this->targetAttribute) > 1) { |
|
105 | // fallback for deprecated `comboNotUnique` property - use it as message if is set |
||
106 | 6 | if ($this->comboNotUnique === null) { |
|
|
|||
107 | 3 | $this->message = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.'); |
|
108 | } else { |
||
109 | 3 | $this->message = $this->comboNotUnique; |
|
110 | } |
||
111 | } else { |
||
112 | 54 | $this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.'); |
|
113 | } |
||
114 | 57 | } |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 42 | public function validateAttribute($model, $attribute) |
|
120 | { |
||
121 | /* @var $targetClass ActiveRecordInterface */ |
||
122 | 42 | $targetClass = $this->getTargetClass($model); |
|
123 | 42 | $targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; |
|
124 | 42 | $rawConditions = $this->prepareConditions($targetAttribute, $model, $attribute); |
|
125 | 42 | $conditions[] = $this->targetAttributeJunction === 'or' ? 'or' : 'and'; |
|
126 | |||
127 | 42 | foreach ($rawConditions as $key => $value) { |
|
128 | 42 | if (is_array($value)) { |
|
129 | 6 | $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); |
|
130 | 6 | return; |
|
131 | } |
||
132 | 39 | $conditions[] = [$key => $value]; |
|
133 | } |
||
134 | |||
135 | 39 | if ($this->modelExists($targetClass, $conditions, $model)) { |
|
136 | 29 | if (count($targetAttribute) > 1) { |
|
137 | 6 | $this->addComboNotUniqueError($model, $attribute); |
|
138 | } else { |
||
139 | 29 | $this->addError($model, $attribute, $this->message); |
|
140 | } |
||
141 | } |
||
142 | 36 | } |
|
143 | |||
144 | /** |
||
145 | * @param Model $model the data model to be validated |
||
146 | * @return string Target class name |
||
147 | */ |
||
148 | 51 | private function getTargetClass($model) |
|
152 | |||
153 | /** |
||
154 | * Checks whether the $model exists in the database. |
||
155 | * |
||
156 | * @param string $targetClass the name of the ActiveRecord class that should be used to validate the uniqueness |
||
157 | * of the current attribute value. |
||
158 | * @param array $conditions conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
159 | * @param Model $model the data model to be validated |
||
160 | * |
||
161 | * @return bool whether the model already exists |
||
162 | */ |
||
163 | 39 | private function modelExists($targetClass, $conditions, $model) |
|
164 | { |
||
165 | /** @var ActiveRecordInterface $targetClass $query */ |
||
166 | 39 | $query = $this->prepareQuery($targetClass, $conditions); |
|
167 | |||
168 | 39 | if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) { |
|
169 | // if current $model isn't in the database yet then it's OK just to call exists() |
||
170 | // also there's no need to run check based on primary keys, when $targetClass is not the same as $model's class |
||
171 | 33 | $exists = $query->exists(); |
|
172 | } else { |
||
173 | // if current $model is in the database already we can't use exists() |
||
174 | 16 | if ($query instanceof \yii\db\ActiveQuery) { |
|
175 | // only select primary key to optimize query |
||
176 | 16 | $columnsCondition = array_flip($targetClass::primaryKey()); |
|
177 | 16 | $query->select(array_flip($this->applyTableAlias($query, $columnsCondition))); |
|
178 | } |
||
179 | 16 | $models = $query->limit(2)->asArray()->all(); |
|
180 | 16 | $n = count($models); |
|
181 | 16 | if ($n === 1) { |
|
182 | // if there is one record, check if it is the currently validated model |
||
183 | 16 | $dbModel = reset($models); |
|
184 | 16 | $pks = $targetClass::primaryKey(); |
|
185 | 16 | $pk = []; |
|
186 | 16 | foreach ($pks as $pkAttribute) { |
|
187 | 16 | $pk[$pkAttribute] = $dbModel[$pkAttribute]; |
|
188 | } |
||
189 | 16 | $exists = ($pk != $model->getOldPrimaryKey(true)); |
|
190 | } else { |
||
191 | // if there is more than one record, the value is not unique |
||
192 | 3 | $exists = $n > 1; |
|
193 | } |
||
194 | } |
||
195 | |||
196 | 36 | return $exists; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Prepares a query by applying filtering conditions defined in $conditions method property |
||
201 | * and [[filter]] class property. |
||
202 | * |
||
203 | * @param ActiveRecordInterface $targetClass the name of the ActiveRecord class that should be used to validate |
||
204 | * the uniqueness of the current attribute value. |
||
205 | * @param array $conditions conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format |
||
206 | * |
||
207 | * @return ActiveQueryInterface|ActiveQuery |
||
208 | */ |
||
209 | 42 | private function prepareQuery($targetClass, $conditions) |
|
221 | |||
222 | /** |
||
223 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
224 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
225 | * |
||
226 | * @param string|array $targetAttribute the name of the [[\yii\db\ActiveRecord|ActiveRecord]] attribute that |
||
227 | * should be used to validate the uniqueness of the current attribute value. You may use an array to validate |
||
228 | * the uniqueness of multiple columns at the same time. The array values are the attributes that will be |
||
229 | * used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. |
||
230 | * If the key and the value are the same, you can just specify the value. |
||
231 | * @param Model $model the data model to be validated |
||
232 | * @param string $attribute the name of the attribute to be validated in the $model |
||
233 | * |
||
234 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
235 | */ |
||
236 | 45 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
253 | |||
254 | /** |
||
255 | * Builds and adds [[comboNotUnique]] error message to the specified model attribute. |
||
256 | * @param \yii\base\Model $model the data model. |
||
257 | * @param string $attribute the name of the attribute. |
||
258 | */ |
||
259 | 6 | private function addComboNotUniqueError($model, $attribute) |
|
277 | |||
278 | /** |
||
279 | * Returns conditions with alias |
||
280 | * @param ActiveQuery $query |
||
281 | * @param array $conditions array of condition, keys to be modified |
||
282 | * @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
||
283 | * @return array |
||
284 | */ |
||
285 | 42 | private function applyTableAlias($query, $conditions, $alias = null) |
|
300 | |||
301 | /** |
||
302 | * Prefix conditions with aliases |
||
303 | * |
||
304 | * @param ActiveRecord $model |
||
305 | * @param array $conditions |
||
306 | * @return array |
||
307 | */ |
||
308 | 42 | private function prefixConditions($model, $conditions) |
|
315 | } |
||
316 |
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.