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
|
|
|
* @var bool whether this validator is forced to always use master DB |
84
|
|
|
* @since 2.0.14 |
85
|
|
|
*/ |
86
|
|
|
public $forceMasterDb = true; |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
28 |
|
public function init() |
93
|
|
|
{ |
94
|
28 |
|
parent::init(); |
95
|
28 |
|
if ($this->message === null) { |
96
|
28 |
|
$this->message = Yii::t('yii', '{attribute} is invalid.'); |
97
|
|
|
} |
98
|
28 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
22 |
|
public function validateAttribute($model, $attribute) |
104
|
|
|
{ |
105
|
22 |
|
if (!empty($this->targetRelation)) { |
106
|
9 |
|
$this->checkTargetRelationExistence($model, $attribute); |
107
|
|
|
} else { |
108
|
13 |
|
$this->checkTargetAttributeExistence($model, $attribute); |
109
|
|
|
} |
110
|
22 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Validates existence of the current attribute based on relation name |
114
|
|
|
* @param \yii\db\ActiveRecord $model the data model to be validated |
115
|
|
|
* @param string $attribute the name of the attribute to be validated. |
116
|
|
|
*/ |
117
|
9 |
|
private function checkTargetRelationExistence($model, $attribute) |
118
|
|
|
{ |
119
|
9 |
|
$exists = false; |
120
|
|
|
/** @var ActiveQuery $relationQuery */ |
121
|
9 |
|
$relationQuery = $model->{'get' . ucfirst($this->targetRelation)}(); |
122
|
|
|
|
123
|
9 |
|
if ($this->filter instanceof \Closure) { |
124
|
3 |
|
call_user_func($this->filter, $relationQuery); |
125
|
6 |
|
} elseif ($this->filter !== null) { |
126
|
|
|
$relationQuery->andWhere($this->filter); |
127
|
|
|
} |
128
|
|
|
|
129
|
9 |
|
if ($this->forceMasterDb && method_exists($model::getDb(), 'useMaster')) { |
130
|
9 |
|
$model::getDb()->useMaster(function() use ($relationQuery, &$exists) { |
131
|
9 |
|
$exists = $relationQuery->exists(); |
132
|
9 |
|
}); |
133
|
|
|
} else { |
134
|
3 |
|
$exists = $relationQuery->exists(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
9 |
|
if (!$exists) { |
139
|
6 |
|
$this->addError($model, $attribute, $this->message); |
140
|
|
|
} |
141
|
9 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Validates existence of the current attribute based on targetAttribute |
145
|
|
|
* @param \yii\base\Model $model the data model to be validated |
146
|
|
|
* @param string $attribute the name of the attribute to be validated. |
147
|
|
|
*/ |
148
|
13 |
|
private function checkTargetAttributeExistence($model, $attribute) |
149
|
|
|
{ |
150
|
13 |
|
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; |
151
|
13 |
|
$params = $this->prepareConditions($targetAttribute, $model, $attribute); |
152
|
13 |
|
$conditions = [$this->targetAttributeJunction == 'or' ? 'or' : 'and']; |
153
|
|
|
|
154
|
13 |
|
if (!$this->allowArray) { |
155
|
13 |
|
foreach ($params as $key => $value) { |
156
|
13 |
|
if (is_array($value)) { |
157
|
3 |
|
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); |
158
|
|
|
|
159
|
3 |
|
return; |
160
|
|
|
} |
161
|
13 |
|
$conditions[] = [$key => $value]; |
162
|
|
|
} |
163
|
|
|
} else { |
164
|
3 |
|
$conditions[] = $params; |
165
|
|
|
} |
166
|
|
|
|
167
|
13 |
|
$targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass; |
168
|
13 |
|
$query = $this->createQuery($targetClass, $conditions); |
169
|
|
|
|
170
|
13 |
|
if (!$this->valueExists($targetClass, $query, $model->$attribute)) { |
171
|
6 |
|
$this->addError($model, $attribute, $this->message); |
172
|
|
|
} |
173
|
13 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
177
|
|
|
* [[\yii\db\Query::where()|Query::where()]] key-value format. |
178
|
|
|
* |
179
|
|
|
* @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
180
|
|
|
* validate the existence of the current attribute value. If not set, it will use the name |
181
|
|
|
* of the attribute currently being validated. You may use an array to validate the existence |
182
|
|
|
* of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
183
|
|
|
* the array value is the name of the database field to search. |
184
|
|
|
* If the key and the value are the same, you can just specify the value. |
185
|
|
|
* @param \yii\base\Model $model the data model to be validated |
186
|
|
|
* @param string $attribute the name of the attribute to be validated in the $model |
187
|
|
|
* @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
188
|
|
|
* @throws InvalidConfigException |
189
|
|
|
*/ |
190
|
13 |
|
private function prepareConditions($targetAttribute, $model, $attribute) |
191
|
|
|
{ |
192
|
13 |
|
if (is_array($targetAttribute)) { |
193
|
9 |
|
if ($this->allowArray) { |
194
|
|
|
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.'); |
195
|
|
|
} |
196
|
9 |
|
$conditions = []; |
197
|
9 |
|
foreach ($targetAttribute as $k => $v) { |
198
|
9 |
|
$conditions[$v] = is_int($k) ? $model->$v : $model->$k; |
199
|
|
|
} |
200
|
|
|
} else { |
201
|
4 |
|
$conditions = [$targetAttribute => $model->$attribute]; |
202
|
|
|
} |
203
|
|
|
|
204
|
13 |
|
$targetModelClass = $this->getTargetClass($model); |
205
|
13 |
|
if (!is_subclass_of($targetModelClass, 'yii\db\ActiveRecord')) { |
206
|
|
|
return $conditions; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** @var ActiveRecord $targetModelClass */ |
210
|
13 |
|
return $this->applyTableAlias($targetModelClass::find(), $conditions); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param Model $model the data model to be validated |
215
|
|
|
* @return string Target class name |
216
|
|
|
*/ |
217
|
13 |
|
private function getTargetClass($model) |
218
|
|
|
{ |
219
|
13 |
|
return $this->targetClass === null ? get_class($model) : $this->targetClass; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
6 |
|
protected function validateValue($value) |
226
|
|
|
{ |
227
|
6 |
|
if ($this->targetClass === null) { |
228
|
3 |
|
throw new InvalidConfigException('The "targetClass" property must be set.'); |
229
|
|
|
} |
230
|
6 |
|
if (!is_string($this->targetAttribute)) { |
231
|
3 |
|
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.'); |
232
|
|
|
} |
233
|
|
|
|
234
|
3 |
|
if (is_array($value) && !$this->allowArray) { |
235
|
3 |
|
return [$this->message, []]; |
236
|
|
|
} |
237
|
|
|
|
238
|
3 |
|
$query = $this->createQuery($this->targetClass, [$this->targetAttribute => $value]); |
239
|
|
|
|
240
|
3 |
|
return $this->valueExists($this->targetClass, $query, $value) ? null : [$this->message, []]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Check whether value exists in target table |
245
|
|
|
* |
246
|
|
|
* @param string $targetClass |
247
|
|
|
* @param QueryInterface $query |
248
|
|
|
* @param mixed $value the value want to be checked |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
16 |
|
private function valueExists($targetClass, $query, $value) |
252
|
|
|
{ |
253
|
16 |
|
$db = $targetClass::getDb(); |
254
|
16 |
|
$exists = false; |
255
|
|
|
|
256
|
16 |
|
if ($this->forceMasterDb && method_exists($db, 'useMaster')) { |
257
|
16 |
|
$db->useMaster(function ($db) use ($query, $value, &$exists) { |
|
|
|
|
258
|
16 |
|
$exists = $this->queryValueExists($query, $value); |
259
|
16 |
|
}); |
260
|
|
|
} else { |
261
|
|
|
$exists = $this->queryValueExists($query, $value); |
262
|
|
|
} |
263
|
|
|
|
264
|
16 |
|
return $exists; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Run query to check if value exists |
270
|
|
|
* |
271
|
|
|
* @param QueryInterface $query |
272
|
|
|
* @param mixed $value the value to be checked |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
16 |
|
private function queryValueExists($query, $value) |
276
|
|
|
{ |
277
|
16 |
|
if (is_array($value)) { |
278
|
3 |
|
return $query->count("DISTINCT [[$this->targetAttribute]]") == count($value) ; |
279
|
|
|
} |
280
|
16 |
|
return $query->exists(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Creates a query instance with the given condition. |
285
|
|
|
* @param string $targetClass the target AR class |
286
|
|
|
* @param mixed $condition query condition |
287
|
|
|
* @return \yii\db\ActiveQueryInterface the query instance |
288
|
|
|
*/ |
289
|
16 |
|
protected function createQuery($targetClass, $condition) |
290
|
|
|
{ |
291
|
|
|
/* @var $targetClass \yii\db\ActiveRecordInterface */ |
292
|
16 |
|
$query = $targetClass::find()->andWhere($condition); |
293
|
16 |
|
if ($this->filter instanceof \Closure) { |
294
|
|
|
call_user_func($this->filter, $query); |
295
|
16 |
|
} elseif ($this->filter !== null) { |
296
|
|
|
$query->andWhere($this->filter); |
297
|
|
|
} |
298
|
|
|
|
299
|
16 |
|
return $query; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns conditions with alias. |
304
|
|
|
* @param ActiveQuery $query |
305
|
|
|
* @param array $conditions array of condition, keys to be modified |
306
|
|
|
* @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
13 |
|
private function applyTableAlias($query, $conditions, $alias = null) |
310
|
|
|
{ |
311
|
13 |
|
if ($alias === null) { |
312
|
13 |
|
$alias = array_keys($query->getTablesUsedInFrom())[0]; |
313
|
|
|
} |
314
|
13 |
|
$prefixedConditions = []; |
315
|
13 |
|
foreach ($conditions as $columnName => $columnValue) { |
316
|
13 |
|
if (strpos($columnName, '(') === false) { |
317
|
10 |
|
$prefixedColumn = "{$alias}.[[" . preg_replace( |
318
|
10 |
|
'/^' . preg_quote($alias) . '\.(.*)$/', |
319
|
10 |
|
'$1', |
320
|
10 |
|
$columnName) . ']]'; |
321
|
|
|
} else { |
322
|
|
|
// there is an expression, can't prefix it reliably |
323
|
3 |
|
$prefixedColumn = $columnName; |
324
|
|
|
} |
325
|
|
|
|
326
|
13 |
|
$prefixedConditions[$prefixedColumn] = $columnValue; |
327
|
|
|
} |
328
|
|
|
|
329
|
13 |
|
return $prefixedConditions; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.