1 | <?php |
||
20 | class StringValidator extends Validator |
||
21 | { |
||
22 | /** |
||
23 | * @var int|array specifies the length limit of the value to be validated. |
||
24 | * This can be specified in one of the following forms: |
||
25 | * |
||
26 | * - an integer: the exact length that the value should be of; |
||
27 | * - an array of one element: the minimum length that the value should be of. For example, `[8]`. |
||
28 | * This will overwrite [[min]]. |
||
29 | * - an array of two elements: the minimum and maximum lengths that the value should be of. |
||
30 | * For example, `[8, 128]`. This will overwrite both [[min]] and [[max]]. |
||
31 | * @see tooShort for the customized message for a too short string. |
||
32 | * @see tooLong for the customized message for a too long string. |
||
33 | * @see notEqual for the customized message for a string that does not match desired length. |
||
34 | */ |
||
35 | public $length; |
||
36 | /** |
||
37 | * @var int maximum length. If not set, it means no maximum length limit. |
||
38 | * @see tooLong for the customized message for a too long string. |
||
39 | */ |
||
40 | public $max; |
||
41 | /** |
||
42 | * @var int minimum length. If not set, it means no minimum length limit. |
||
43 | * @see tooShort for the customized message for a too short string. |
||
44 | */ |
||
45 | public $min; |
||
46 | /** |
||
47 | * @var string user-defined error message used when the value is not a string. |
||
48 | */ |
||
49 | public $message; |
||
50 | /** |
||
51 | * @var string user-defined error message used when the length of the value is smaller than [[min]]. |
||
52 | */ |
||
53 | public $tooShort; |
||
54 | /** |
||
55 | * @var string user-defined error message used when the length of the value is greater than [[max]]. |
||
56 | */ |
||
57 | public $tooLong; |
||
58 | /** |
||
59 | * @var string user-defined error message used when the length of the value is not equal to [[length]]. |
||
60 | */ |
||
61 | public $notEqual; |
||
62 | /** |
||
63 | * @var string the encoding of the string value to be validated (e.g. 'UTF-8'). |
||
64 | * If this property is not set, [[\yii\base\Application::charset]] will be used. |
||
65 | */ |
||
66 | public $encoding; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | 21 | public function init() |
|
73 | { |
||
74 | 21 | parent::init(); |
|
75 | 21 | if (is_array($this->length)) { |
|
76 | 1 | if (isset($this->length[0])) { |
|
77 | 1 | $this->min = $this->length[0]; |
|
78 | } |
||
79 | 1 | if (isset($this->length[1])) { |
|
80 | 1 | $this->max = $this->length[1]; |
|
81 | } |
||
82 | 1 | $this->length = null; |
|
83 | } |
||
84 | 21 | if ($this->encoding === null) { |
|
85 | 21 | $this->encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
|
86 | } |
||
87 | 21 | if ($this->message === null) { |
|
88 | 21 | $this->message = Yii::t('yii', '{attribute} must be a string.'); |
|
89 | } |
||
90 | 21 | if ($this->min !== null && $this->tooShort === null) { |
|
91 | 4 | $this->tooShort = Yii::t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.'); |
|
92 | } |
||
93 | 21 | if ($this->max !== null && $this->tooLong === null) { |
|
94 | 16 | $this->tooLong = Yii::t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.'); |
|
95 | } |
||
96 | 21 | if ($this->length !== null && $this->notEqual === null) { |
|
97 | 2 | $this->notEqual = Yii::t('yii', '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.'); |
|
98 | } |
||
99 | 21 | } |
|
100 | |||
101 | /** |
||
102 | * @inheritdoc |
||
103 | */ |
||
104 | 7 | public function validateAttribute($model, $attribute) |
|
105 | { |
||
106 | 7 | $value = $model->$attribute; |
|
107 | |||
108 | 7 | if (!is_string($value)) { |
|
109 | 1 | $this->addError($model, $attribute, $this->message); |
|
110 | |||
111 | 1 | return; |
|
112 | } |
||
113 | |||
114 | 7 | $length = mb_strlen($value, $this->encoding); |
|
115 | |||
116 | 7 | if ($this->min !== null && $length < $this->min) { |
|
117 | 1 | $this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]); |
|
118 | } |
||
119 | 7 | if ($this->max !== null && $length > $this->max) { |
|
120 | 4 | $this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]); |
|
121 | } |
||
122 | 7 | if ($this->length !== null && $length !== $this->length) { |
|
123 | 1 | $this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]); |
|
124 | } |
||
125 | 7 | } |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 3 | protected function validateValue($value) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | public function clientValidateAttribute($model, $attribute, $view) |
||
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | public function getClientOptions($model, $attribute) |
||
202 | } |
||
203 |