forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRangeValidator.php
133 lines (119 loc) · 3.67 KB
/
RangeValidator.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* RangeValidator validates that the attribute value is among a list of values.
*
* The range can be specified via the [[range]] property.
* If the [[not]] property is set true, the validator will ensure the attribute value
* is NOT among the specified range.
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class RangeValidator extends Validator
{
/**
* @var array|\Traversable|\Closure a list of valid values that the attribute value should be among or an anonymous function that returns
* such a list. The signature of the anonymous function should be as follows,
*
* ```php
* function($model, $attribute) {
* // compute range
* return $range;
* }
* ```
*/
public $range;
/**
* @var boolean whether the comparison is strict (both type and value must be the same)
*/
public $strict = false;
/**
* @var boolean whether to invert the validation logic. Defaults to false. If set to true,
* the attribute value should NOT be among the list of values defined via [[range]].
*/
public $not = false;
/**
* @var boolean whether to allow array type attribute.
*/
public $allowArray = false;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!is_array($this->range)
&& !($this->range instanceof \Closure)
&& !($this->range instanceof \Traversable)
) {
throw new InvalidConfigException('The "range" property must be set.');
}
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
/**
* @inheritdoc
*/
protected function validateValue($value)
{
$in = false;
if ($this->allowArray
&& ($value instanceof \Traversable || is_array($value))
&& ArrayHelper::isSubset($value, $this->range, $this->strict)
) {
$in = true;
}
if (!$in && ArrayHelper::isIn($value, $this->range, $this->strict)) {
$in = true;
}
return $this->not !== $in ? null : [$this->message, []];
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
if ($this->range instanceof \Closure) {
$this->range = call_user_func($this->range, $model, $attribute);
}
parent::validateAttribute($model, $attribute);
}
/**
* @inheritdoc
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->range instanceof \Closure) {
$this->range = call_user_func($this->range, $model, $attribute);
}
$range = [];
foreach ($this->range as $value) {
$range[] = (string) $value;
}
$options = [
'range' => $range,
'not' => $this->not,
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language),
];
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
}
if ($this->allowArray) {
$options['allowArray'] = 1;
}
ValidationAsset::register($view);
return 'yii.validation.range(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
}
}