1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\validators; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\db\ActiveQuery; |
14
|
|
|
use yii\db\ActiveRecord; |
15
|
|
|
use yii\db\QueryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ExistValidator validates that the attribute value exists in a table. |
19
|
|
|
* |
20
|
|
|
* ExistValidator checks if the value being validated can be found in the table column specified by |
21
|
|
|
* the ActiveRecord class [[targetClass]] and the attribute [[targetAttribute]]. |
22
|
|
|
* |
23
|
|
|
* This validator is often used to verify that a foreign key contains a value |
24
|
|
|
* that can be found in the foreign table. |
25
|
|
|
* |
26
|
|
|
* The following are examples of validation rules using this validator: |
27
|
|
|
* |
28
|
|
|
* ```php |
29
|
|
|
* // a1 needs to exist |
30
|
|
|
* ['a1', 'exist'] |
31
|
|
|
* // a1 needs to exist, but its value will use a2 to check for the existence |
32
|
|
|
* ['a1', 'exist', 'targetAttribute' => 'a2'] |
33
|
|
|
* // a1 and a2 need to exist together, and they both will receive error message |
34
|
|
|
* [['a1', 'a2'], 'exist', 'targetAttribute' => ['a1', 'a2']] |
35
|
|
|
* // a1 and a2 need to exist together, only a1 will receive error message |
36
|
|
|
* ['a1', 'exist', 'targetAttribute' => ['a1', 'a2']] |
37
|
|
|
* // a1 needs to exist by checking the existence of both a2 and a3 (using a1 value) |
38
|
|
|
* ['a1', 'exist', 'targetAttribute' => ['a2', 'a1' => 'a3']] |
39
|
|
|
* ``` |
40
|
|
|
* |
41
|
|
|
* @author Qiang Xue <[email protected]> |
42
|
|
|
* @since 2.0 |
43
|
|
|
*/ |
44
|
|
|
class ExistValidator extends Validator |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var string the name of the ActiveRecord class that should be used to validate the existence |
48
|
|
|
* of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated. |
49
|
|
|
* @see targetAttribute |
50
|
|
|
*/ |
51
|
|
|
public $targetClass; |
52
|
|
|
/** |
53
|
|
|
* @var string|array the name of the ActiveRecord attribute that should be used to |
54
|
|
|
* validate the existence of the current attribute value. If not set, it will use the name |
55
|
|
|
* of the attribute currently being validated. You may use an array to validate the existence |
56
|
|
|
* of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
57
|
|
|
* the array value is the name of the database field to search. |
58
|
|
|
*/ |
59
|
|
|
public $targetAttribute; |
60
|
|
|
/** |
61
|
|
|
* @var string the name of the relation that should be used to validate the existence of the current attribute value |
62
|
|
|
* This param overwrites $targetClass and $targetAttribute |
63
|
|
|
* @since 2.0.14 |
64
|
|
|
*/ |
65
|
|
|
public $targetRelation; |
66
|
|
|
/** |
67
|
|
|
* @var string|array|\Closure additional filter to be applied to the DB query used to check the existence of the attribute value. |
68
|
|
|
* This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
69
|
|
|
* on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
70
|
|
|
* is the [[\yii\db\Query|Query]] object that you can modify in the function. |
71
|
|
|
*/ |
72
|
|
|
public $filter; |
73
|
|
|
/** |
74
|
|
|
* @var bool whether to allow array type attribute. |
75
|
|
|
*/ |
76
|
|
|
public $allowArray = false; |
77
|
|
|
/** |
78
|
|
|
* @var string and|or define how target attributes are related |
79
|
|
|
* @since 2.0.11 |
80
|
|
|
*/ |
81
|
|
|
public $targetAttributeJunction = 'and'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var bool whether this validator is forced to always use master DB |
85
|
|
|
* @since 2.0.14 |
86
|
|
|
*/ |
87
|
|
|
public $forceMasterDb = true; |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
25 |
|
public function init() |
94
|
|
|
{ |
95
|
25 |
|
parent::init(); |
96
|
25 |
|
if ($this->message === null) { |
97
|
25 |
|
$this->message = Yii::t('yii', '{attribute} is invalid.'); |
98
|
|
|
} |
99
|
25 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
19 |
|
public function validateAttribute($model, $attribute) |
105
|
|
|
{ |
106
|
19 |
|
if (!empty($this->targetRelation)) { |
107
|
6 |
|
$this->checkTargetRelationExistence($model, $attribute); |
108
|
|
|
} else { |
109
|
13 |
|
$this->checkTargetAttributeExistence($model, $attribute); |
110
|
|
|
} |
111
|
19 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Validates existence of the current attribute based on relation name. |
115
|
|
|
* @param \yii\db\ActiveRecord $model the data model to be validated |
116
|
|
|
* @param string $attribute the name of the attribute to be validated. |
117
|
|
|
*/ |
118
|
6 |
|
private function checkTargetRelationExistence($model, $attribute) |
119
|
|
|
{ |
120
|
6 |
|
$exists = false; |
121
|
|
|
/** @var ActiveQuery $relationQuery */ |
122
|
6 |
|
$relationQuery = $model->{'get' . ucfirst($this->targetRelation)}(); |
123
|
|
|
|
124
|
6 |
|
if ($this->filter instanceof \Closure) { |
125
|
|
|
call_user_func($this->filter, $relationQuery); |
126
|
6 |
|
} elseif ($this->filter !== null) { |
127
|
|
|
$relationQuery->andWhere($this->filter); |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
if ($this->forceMasterDb && method_exists($model::getDb(), 'useMaster')) { |
131
|
6 |
|
$model::getDb()->useMaster(function () use ($relationQuery, &$exists) { |
132
|
6 |
|
$exists = $relationQuery->exists(); |
133
|
6 |
|
}); |
134
|
|
|
} else { |
135
|
3 |
|
$relationQuery->exists(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
6 |
|
if (!$exists) { |
140
|
3 |
|
$this->addError($model, $attribute, $this->message); |
141
|
|
|
} |
142
|
6 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Validates existence of the current attribute based on targetAttribute. |
146
|
|
|
* @param \yii\base\Model $model the data model to be validated |
147
|
|
|
* @param string $attribute the name of the attribute to be validated. |
148
|
|
|
*/ |
149
|
13 |
|
private function checkTargetAttributeExistence($model, $attribute) |
150
|
|
|
{ |
151
|
13 |
|
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; |
152
|
13 |
|
$params = $this->prepareConditions($targetAttribute, $model, $attribute); |
153
|
13 |
|
$conditions = [$this->targetAttributeJunction == 'or' ? 'or' : 'and']; |
154
|
|
|
|
155
|
13 |
|
if (!$this->allowArray) { |
156
|
13 |
|
foreach ($params as $key => $value) { |
157
|
13 |
|
if (is_array($value)) { |
158
|
3 |
|
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); |
159
|
|
|
|
160
|
3 |
|
return; |
161
|
|
|
} |
162
|
13 |
|
$conditions[] = [$key => $value]; |
163
|
|
|
} |
164
|
|
|
} else { |
165
|
3 |
|
$conditions[] = $params; |
166
|
|
|
} |
167
|
|
|
|
168
|
13 |
|
$targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass; |
169
|
13 |
|
$query = $this->createQuery($targetClass, $conditions); |
170
|
|
|
|
171
|
13 |
|
if (!$this->valueExists($targetClass, $query, $model->$attribute)) { |
172
|
6 |
|
$this->addError($model, $attribute, $this->message); |
173
|
|
|
} |
174
|
13 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
178
|
|
|
* [[\yii\db\Query::where()|Query::where()]] key-value format. |
179
|
|
|
* |
180
|
|
|
* @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
181
|
|
|
* validate the existence of the current attribute value. If not set, it will use the name |
182
|
|
|
* of the attribute currently being validated. You may use an array to validate the existence |
183
|
|
|
* of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
184
|
|
|
* the array value is the name of the database field to search. |
185
|
|
|
* If the key and the value are the same, you can just specify the value. |
186
|
|
|
* @param \yii\base\Model $model the data model to be validated |
187
|
|
|
* @param string $attribute the name of the attribute to be validated in the $model |
188
|
|
|
* @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
189
|
|
|
* @throws InvalidConfigException |
190
|
|
|
*/ |
191
|
13 |
|
private function prepareConditions($targetAttribute, $model, $attribute) |
192
|
|
|
{ |
193
|
13 |
|
if (is_array($targetAttribute)) { |
194
|
9 |
|
if ($this->allowArray) { |
195
|
|
|
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.'); |
196
|
|
|
} |
197
|
9 |
|
$conditions = []; |
198
|
9 |
|
foreach ($targetAttribute as $k => $v) { |
199
|
9 |
|
$conditions[$v] = is_int($k) ? $model->$v : $model->$k; |
200
|
|
|
} |
201
|
|
|
} else { |
202
|
4 |
|
$conditions = [$targetAttribute => $model->$attribute]; |
203
|
|
|
} |
204
|
|
|
|
205
|
13 |
|
$targetModelClass = $this->getTargetClass($model); |
206
|
13 |
|
if (!is_subclass_of($targetModelClass, 'yii\db\ActiveRecord')) { |
207
|
|
|
return $conditions; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** @var ActiveRecord $targetModelClass */ |
211
|
13 |
|
return $this->applyTableAlias($targetModelClass::find(), $conditions); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param Model $model the data model to be validated |
216
|
|
|
* @return string Target class name |
217
|
|
|
*/ |
218
|
13 |
|
private function getTargetClass($model) |
219
|
|
|
{ |
220
|
13 |
|
return $this->targetClass === null ? get_class($model) : $this->targetClass; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritdoc} |
225
|
|
|
*/ |
226
|
6 |
|
protected function validateValue($value) |
227
|
|
|
{ |
228
|
6 |
|
if ($this->targetClass === null) { |
229
|
3 |
|
throw new InvalidConfigException('The "targetClass" property must be set.'); |
230
|
|
|
} |
231
|
6 |
|
if (!is_string($this->targetAttribute)) { |
232
|
3 |
|
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.'); |
233
|
|
|
} |
234
|
|
|
|
235
|
3 |
|
if (is_array($value) && !$this->allowArray) { |
236
|
3 |
|
return [$this->message, []]; |
237
|
|
|
} |
238
|
|
|
|
239
|
3 |
|
$query = $this->createQuery($this->targetClass, [$this->targetAttribute => $value]); |
240
|
|
|
|
241
|
3 |
|
return $this->valueExists($this->targetClass, $query, $value) ? null : [$this->message, []]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Check whether value exists in target table. |
246
|
|
|
* |
247
|
|
|
* @param string $targetClass |
248
|
|
|
* @param QueryInterface $query |
249
|
|
|
* @param mixed $value the value want to be checked |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
16 |
|
private function valueExists($targetClass, $query, $value) |
253
|
|
|
{ |
254
|
16 |
|
$db = $targetClass::getDb(); |
255
|
16 |
|
$exists = false; |
256
|
|
|
|
257
|
16 |
|
if ($this->forceMasterDb && method_exists($db, 'useMaster')) { |
258
|
16 |
|
$db->useMaster(function ($db) use ($query, $value, &$exists) { |
|
|
|
|
259
|
16 |
|
$exists = $this->queryValueExists($query, $value); |
260
|
16 |
|
}); |
261
|
|
|
} else { |
262
|
|
|
$exists = $this->queryValueExists($query, $value); |
263
|
|
|
} |
264
|
|
|
|
265
|
16 |
|
return $exists; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Run query to check if value exists. |
271
|
|
|
* |
272
|
|
|
* @param QueryInterface $query |
273
|
|
|
* @param mixed $value the value to be checked |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
16 |
|
private function queryValueExists($query, $value) |
277
|
|
|
{ |
278
|
16 |
|
if (is_array($value)) { |
279
|
3 |
|
return $query->count("DISTINCT [[$this->targetAttribute]]") == count($value); |
280
|
|
|
} |
281
|
16 |
|
return $query->exists(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Creates a query instance with the given condition. |
286
|
|
|
* @param string $targetClass the target AR class |
287
|
|
|
* @param mixed $condition query condition |
288
|
|
|
* @return \yii\db\ActiveQueryInterface the query instance |
289
|
|
|
*/ |
290
|
16 |
|
protected function createQuery($targetClass, $condition) |
291
|
|
|
{ |
292
|
|
|
/* @var $targetClass \yii\db\ActiveRecordInterface */ |
293
|
16 |
|
$query = $targetClass::find()->andWhere($condition); |
294
|
16 |
|
if ($this->filter instanceof \Closure) { |
295
|
|
|
call_user_func($this->filter, $query); |
296
|
16 |
|
} elseif ($this->filter !== null) { |
297
|
|
|
$query->andWhere($this->filter); |
298
|
|
|
} |
299
|
|
|
|
300
|
16 |
|
return $query; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Returns conditions with alias. |
305
|
|
|
* @param ActiveQuery $query |
306
|
|
|
* @param array $conditions array of condition, keys to be modified |
307
|
|
|
* @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
13 |
|
private function applyTableAlias($query, $conditions, $alias = null) |
311
|
|
|
{ |
312
|
13 |
|
if ($alias === null) { |
313
|
13 |
|
$alias = array_keys($query->getTablesUsedInFrom())[0]; |
314
|
|
|
} |
315
|
13 |
|
$prefixedConditions = []; |
316
|
13 |
|
foreach ($conditions as $columnName => $columnValue) { |
317
|
13 |
|
if (strpos($columnName, '(') === false) { |
318
|
10 |
|
$prefixedColumn = "{$alias}.[[" . preg_replace( |
319
|
10 |
|
'/^' . preg_quote($alias) . '\.(.*)$/', |
320
|
10 |
|
'$1', |
321
|
10 |
|
$columnName |
322
|
10 |
|
) . ']]'; |
323
|
|
|
} else { |
324
|
|
|
// there is an expression, can't prefix it reliably |
325
|
3 |
|
$prefixedColumn = $columnName; |
326
|
|
|
} |
327
|
|
|
|
328
|
13 |
|
$prefixedConditions[$prefixedColumn] = $columnValue; |
329
|
|
|
} |
330
|
|
|
|
331
|
13 |
|
return $prefixedConditions; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.