-
Notifications
You must be signed in to change notification settings - Fork 10
/
RelationUniqueValidator.php
85 lines (77 loc) · 2.53 KB
/
RelationUniqueValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace mdm\behaviors\ar;
use Yii;
use yii\validators\Validator;
/**
* Description of RelationUniqueValidator
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class RelationUniqueValidator extends Validator
{
/**
* @var string|array the name of the ActiveRecord attribute that should be used to
* validate the uniqueness of the current attribute value. If not set, it will use the name
* of the attribute currently being validated. You may use an array to validate the uniqueness
* of multiple columns at the same time. The array values are the attributes that will be
* used to validate the uniqueness, while the array keys are the attributes whose values are to be validated.
* If the key and the value are the same, you can just specify the value.
*/
public $targetAttributes;
/**
*
* @var boolean
*/
public $checkNull = false;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} not unique.');
}
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
if ($this->targetAttributes === null) {
$relation = $model->getRelation($attribute);
$class = $relation->modelClass;
$targetAttributes = $class::primaryKey();
} else {
$targetAttributes = $this->targetAttributes;
}
$values = [];
$related = $model->$attribute;
if (!empty($related)) {
if (is_array($targetAttributes)) {
foreach ($related as $child) {
$m = [];
foreach ($targetAttributes as $attr) {
$m[$attr] = $child[$attr];
}
$m = md5(serialize($m));
if (isset($values[$m])) {
$this->addError($model, $attribute, $this->message);
return;
}
$values[$m] = true;
}
} else {
foreach ($related as $child) {
$m = $child[$targetAttributes];
if ((isset($m) || $this->checkNull) && isset($values[$m])) {
$this->addError($model, $attribute, $this->message);
return;
}
$values[$m] = true;
}
}
}
}
}