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\db\ActiveRecordInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* UniqueValidator validates that the attribute value is unique in the specified database table. |
15
|
|
|
* |
16
|
|
|
* UniqueValidator checks if the value being validated is unique in the table column specified by |
17
|
|
|
* the ActiveRecord class [[targetClass]] and the attribute [[targetAttribute]]. |
18
|
|
|
* |
19
|
|
|
* The following are examples of validation rules using this validator: |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* // a1 needs to be unique |
23
|
|
|
* ['a1', 'unique'] |
24
|
|
|
* // a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value |
25
|
|
|
* ['a1', 'unique', 'targetAttribute' => 'a2'] |
26
|
|
|
* // a1 and a2 need to be unique together, and they both will receive error message |
27
|
|
|
* [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']] |
28
|
|
|
* // a1 and a2 need to be unique together, only a1 will receive error message |
29
|
|
|
* ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] |
30
|
|
|
* // a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value) |
31
|
|
|
* ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']] |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* @author Qiang Xue <[email protected]> |
35
|
|
|
* @since 2.0 |
36
|
|
|
*/ |
37
|
|
|
class UniqueValidator extends Validator |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string the name of the ActiveRecord class that should be used to validate the uniqueness |
41
|
|
|
* of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated. |
42
|
|
|
* @see targetAttribute |
43
|
|
|
*/ |
44
|
|
|
public $targetClass; |
45
|
|
|
/** |
46
|
|
|
* @var string|array the name of the ActiveRecord attribute that should be used to |
47
|
|
|
* validate the uniqueness of the current attribute value. If not set, it will use the name |
48
|
|
|
* of the attribute currently being validated. You may use an array to validate the uniqueness |
49
|
|
|
* of multiple columns at the same time. The array values are the attributes that will be |
50
|
|
|
* used to validate the uniqueness, while the array keys are the attributes whose values are to be validated. |
51
|
|
|
* If the key and the value are the same, you can just specify the value. |
52
|
|
|
*/ |
53
|
|
|
public $targetAttribute; |
54
|
|
|
/** |
55
|
|
|
* @var string|array|\Closure additional filter to be applied to the DB query used to check the uniqueness of the attribute value. |
56
|
|
|
* This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
57
|
|
|
* on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
58
|
|
|
* is the [[\yii\db\Query|Query]] object that you can modify in the function. |
59
|
|
|
*/ |
60
|
|
|
public $filter; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
33 |
|
public function init() |
67
|
|
|
{ |
68
|
33 |
|
parent::init(); |
69
|
33 |
|
if ($this->message === null) { |
70
|
33 |
|
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.'); |
71
|
33 |
|
} |
72
|
33 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
30 |
|
public function validateAttribute($model, $attribute) |
78
|
|
|
{ |
79
|
|
|
/* @var $targetClass ActiveRecordInterface */ |
80
|
30 |
|
$targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass; |
81
|
30 |
|
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; |
82
|
|
|
|
83
|
30 |
|
if (is_array($targetAttribute)) { |
84
|
9 |
|
$params = []; |
85
|
9 |
|
foreach ($targetAttribute as $k => $v) { |
86
|
9 |
|
$params[$v] = is_int($k) ? $model->$v : $model->$k; |
87
|
9 |
|
} |
88
|
9 |
|
} else { |
89
|
24 |
|
$params = [$targetAttribute => $model->$attribute]; |
90
|
|
|
} |
91
|
|
|
|
92
|
30 |
|
foreach ($params as $value) { |
93
|
30 |
|
if (is_array($value)) { |
94
|
6 |
|
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.')); |
95
|
|
|
|
96
|
6 |
|
return; |
97
|
|
|
} |
98
|
27 |
|
} |
99
|
|
|
|
100
|
27 |
|
$query = $targetClass::find(); |
101
|
27 |
|
$query->andWhere($params); |
102
|
|
|
|
103
|
27 |
|
if ($this->filter instanceof \Closure) { |
104
|
|
|
call_user_func($this->filter, $query); |
105
|
27 |
|
} elseif ($this->filter !== null) { |
106
|
|
|
$query->andWhere($this->filter); |
107
|
|
|
} |
108
|
|
|
|
109
|
27 |
|
if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) { |
110
|
|
|
// if current $model isn't in the database yet then it's OK just to call exists() |
111
|
|
|
// also there's no need to run check based on primary keys, when $targetClass is not the same as $model's class |
112
|
24 |
|
$exists = $query->exists(); |
113
|
21 |
|
} else { |
114
|
|
|
// if current $model is in the database already we can't use exists() |
115
|
|
|
/* @var $models ActiveRecordInterface[] */ |
116
|
10 |
|
$models = $query->limit(2)->all(); |
117
|
10 |
|
$n = count($models); |
118
|
10 |
|
if ($n === 1) { |
119
|
10 |
|
$keys = array_keys($params); |
120
|
10 |
|
$pks = $targetClass::primaryKey(); |
121
|
10 |
|
sort($keys); |
122
|
10 |
|
sort($pks); |
123
|
10 |
|
if ($keys === $pks) { |
124
|
|
|
// primary key is modified and not unique |
125
|
6 |
|
$exists = $model->getOldPrimaryKey() != $model->getPrimaryKey(); |
126
|
6 |
|
} else { |
127
|
|
|
// non-primary key, need to exclude the current record based on PK |
128
|
7 |
|
$exists = $models[0]->getPrimaryKey() != $model->getOldPrimaryKey(); |
129
|
|
|
} |
130
|
10 |
|
} else { |
131
|
3 |
|
$exists = $n > 1; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
24 |
|
if ($exists) { |
136
|
20 |
|
$this->addError($model, $attribute, $this->message); |
137
|
20 |
|
} |
138
|
24 |
|
} |
139
|
|
|
} |
140
|
|
|
|