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\ActiveRecord; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ExistValidator validates that the attribute value exists in a table. |
17
|
|
|
* |
18
|
|
|
* ExistValidator checks if the value being validated can be found in the table column specified by |
19
|
|
|
* the ActiveRecord class [[targetClass]] and the attribute [[targetAttribute]]. |
20
|
|
|
* |
21
|
|
|
* This validator is often used to verify that a foreign key contains a value |
22
|
|
|
* that can be found in the foreign table. |
23
|
|
|
* |
24
|
|
|
* The following are examples of validation rules using this validator: |
25
|
|
|
* |
26
|
|
|
* ```php |
27
|
|
|
* // a1 needs to exist |
28
|
|
|
* ['a1', 'exist'] |
29
|
|
|
* // a1 needs to exist, but its value will use a2 to check for the existence |
30
|
|
|
* ['a1', 'exist', 'targetAttribute' => 'a2'] |
31
|
|
|
* // a1 and a2 need to exist together, and they both will receive error message |
32
|
|
|
* [['a1', 'a2'], 'exist', 'targetAttribute' => ['a1', 'a2']] |
33
|
|
|
* // a1 and a2 need to exist together, only a1 will receive error message |
34
|
|
|
* ['a1', 'exist', 'targetAttribute' => ['a1', 'a2']] |
35
|
|
|
* // a1 needs to exist by checking the existence of both a2 and a3 (using a1 value) |
36
|
|
|
* ['a1', 'exist', 'targetAttribute' => ['a2', 'a1' => 'a3']] |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* @author Qiang Xue <[email protected]> |
40
|
|
|
* @since 2.0 |
41
|
|
|
*/ |
42
|
|
|
class ExistValidator extends Validator |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var string the name of the ActiveRecord class that should be used to validate the existence |
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 ActiveRecord attribute that should be used to |
52
|
|
|
* validate the existence 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 existence |
54
|
|
|
* of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
55
|
|
|
* the array value is the name of the database field to search. |
56
|
|
|
*/ |
57
|
|
|
public $targetAttribute; |
58
|
|
|
/** |
59
|
|
|
* @var string|array|\Closure additional filter to be applied to the DB query used to check the existence 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 bool whether to allow array type attribute. |
67
|
|
|
*/ |
68
|
|
|
public $allowArray = false; |
69
|
|
|
/** |
70
|
|
|
* @var string and|or define how target attributes are related |
71
|
|
|
* @since 2.0.11 |
72
|
|
|
*/ |
73
|
|
|
public $targetAttributeJunction = 'and'; |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
12 |
|
public function init() |
80
|
|
|
{ |
81
|
12 |
|
parent::init(); |
82
|
12 |
|
if ($this->message === null) { |
83
|
12 |
|
$this->message = Yii::t('yii', '{attribute} is invalid.'); |
84
|
|
|
} |
85
|
12 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
6 |
|
public function validateAttribute($model, $attribute) |
91
|
|
|
{ |
92
|
6 |
|
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; |
93
|
6 |
|
$params = $this->prepareConditions($targetAttribute, $model, $attribute); |
94
|
6 |
|
$conditions[] = $this->targetAttributeJunction == 'or' ? 'or' : 'and'; |
|
|
|
|
95
|
|
|
|
96
|
6 |
|
if (!$this->allowArray) { |
97
|
6 |
|
foreach ($params as $key => $value) { |
98
|
6 |
|
if (is_array($value)) { |
99
|
3 |
|
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); |
100
|
|
|
|
101
|
3 |
|
return; |
102
|
|
|
} |
103
|
6 |
|
$conditions[] = [$key => $value]; |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
3 |
|
$conditions[] = $params; |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
$targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass; |
110
|
6 |
|
$query = $this->createQuery($targetClass, $conditions); |
111
|
|
|
|
112
|
6 |
|
if (is_array($model->$attribute)) { |
113
|
3 |
|
if ($query->count("DISTINCT [[$targetAttribute]]") != count($model->$attribute)) { |
114
|
3 |
|
$this->addError($model, $attribute, $this->message); |
115
|
|
|
} |
116
|
6 |
|
} elseif (!$query->exists()) { |
117
|
6 |
|
$this->addError($model, $attribute, $this->message); |
118
|
|
|
} |
119
|
6 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
123
|
|
|
* [[\yii\db\Query::where()|Query::where()]] key-value format. |
124
|
|
|
* |
125
|
|
|
* @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
126
|
|
|
* validate the existence of the current attribute value. If not set, it will use the name |
127
|
|
|
* of the attribute currently being validated. You may use an array to validate the existence |
128
|
|
|
* of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
129
|
|
|
* the array value is the name of the database field to search. |
130
|
|
|
* If the key and the value are the same, you can just specify the value. |
131
|
|
|
* @param \yii\base\Model $model the data model to be validated |
132
|
|
|
* @param string $attribute the name of the attribute to be validated in the $model |
133
|
|
|
* @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
134
|
|
|
* @throws InvalidConfigException |
135
|
|
|
*/ |
136
|
6 |
|
private function prepareConditions($targetAttribute, $model, $attribute) |
137
|
|
|
{ |
138
|
6 |
|
if (is_array($targetAttribute)) { |
139
|
3 |
|
if ($this->allowArray) { |
140
|
|
|
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.'); |
141
|
|
|
} |
142
|
3 |
|
$conditions = []; |
143
|
3 |
|
foreach ($targetAttribute as $k => $v) { |
144
|
3 |
|
$conditions[$v] = is_int($k) ? $model->$v : $model->$k; |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
3 |
|
$conditions = [$targetAttribute => $model->$attribute]; |
148
|
|
|
} |
149
|
|
|
|
150
|
6 |
|
if (!$model instanceof ActiveRecord) { |
151
|
|
|
return $conditions; |
152
|
|
|
} |
153
|
|
|
|
154
|
6 |
|
return $this->prefixConditions($model, $conditions); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Model $model the data model to be validated |
159
|
|
|
* @return string Target class name |
160
|
|
|
*/ |
161
|
6 |
|
private function getTargetClass($model) |
162
|
|
|
{ |
163
|
6 |
|
return $this->targetClass === null ? get_class($model) : $this->targetClass; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritdoc |
168
|
|
|
*/ |
169
|
6 |
|
protected function validateValue($value) |
170
|
|
|
{ |
171
|
6 |
|
if ($this->targetClass === null) { |
172
|
3 |
|
throw new InvalidConfigException('The "targetClass" property must be set.'); |
173
|
|
|
} |
174
|
6 |
|
if (!is_string($this->targetAttribute)) { |
175
|
3 |
|
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.'); |
176
|
|
|
} |
177
|
|
|
|
178
|
3 |
|
$query = $this->createQuery($this->targetClass, [$this->targetAttribute => $value]); |
179
|
|
|
|
180
|
3 |
|
if (is_array($value)) { |
181
|
3 |
|
if (!$this->allowArray) { |
182
|
3 |
|
return [$this->message, []]; |
183
|
|
|
} |
184
|
|
|
return $query->count("DISTINCT [[$this->targetAttribute]]") == count($value) ? null : [$this->message, []]; |
185
|
|
|
} else { |
186
|
3 |
|
return $query->exists() ? null : [$this->message, []]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Creates a query instance with the given condition. |
192
|
|
|
* @param string $targetClass the target AR class |
193
|
|
|
* @param mixed $condition query condition |
194
|
|
|
* @return \yii\db\ActiveQueryInterface the query instance |
195
|
|
|
*/ |
196
|
9 |
|
protected function createQuery($targetClass, $condition) |
197
|
|
|
{ |
198
|
|
|
/* @var $targetClass \yii\db\ActiveRecordInterface */ |
199
|
9 |
|
$query = $targetClass::find()->andWhere($condition); |
200
|
9 |
|
if ($this->filter instanceof \Closure) { |
201
|
|
|
call_user_func($this->filter, $query); |
202
|
9 |
|
} elseif ($this->filter !== null) { |
203
|
|
|
$query->andWhere($this->filter); |
204
|
|
|
} |
205
|
|
|
|
206
|
9 |
|
return $query; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Prefix conditions with aliases |
211
|
|
|
* |
212
|
|
|
* @param ActiveRecord $model |
213
|
|
|
* @param array $conditions |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
6 |
|
private function prefixConditions($model, $conditions) |
217
|
|
|
{ |
218
|
6 |
|
$targetModelClass = $this->getTargetClass($model); |
219
|
|
|
|
220
|
|
|
/** @var ActiveRecord $targetModelClass */ |
221
|
6 |
|
$query = $targetModelClass::find(); |
222
|
6 |
|
$tableAliases = array_keys($query->getTablesUsedInFrom()); |
223
|
6 |
|
$primaryTableAlias = $tableAliases[0]; |
224
|
6 |
|
$prefixedConditions = []; |
225
|
6 |
|
foreach ($conditions as $columnName => $columnValue) { |
226
|
6 |
|
$prefixedColumn = "{{{$primaryTableAlias}}}.[[{$columnName}]]"; |
227
|
6 |
|
$prefixedConditions[$prefixedColumn] = $columnValue; |
228
|
|
|
} |
229
|
|
|
|
230
|
6 |
|
return $prefixedConditions; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.