1 | <?php |
||
34 | class CompareValidator extends Validator |
||
35 | { |
||
36 | /** |
||
37 | * Constant for specifying the comparison [[type]] by numeric values. |
||
38 | * @since 2.0.11 |
||
39 | * @see type |
||
40 | */ |
||
41 | const TYPE_STRING = 'string'; |
||
42 | /** |
||
43 | * Constant for specifying the comparison [[type]] by numeric values. |
||
44 | * @since 2.0.11 |
||
45 | * @see type |
||
46 | */ |
||
47 | const TYPE_NUMBER = 'number'; |
||
48 | |||
49 | /** |
||
50 | * @var string the name of the attribute to be compared with. When both this property |
||
51 | * and [[compareValue]] are set, the latter takes precedence. If neither is set, |
||
52 | * it assumes the comparison is against another attribute whose name is formed by |
||
53 | * appending '_repeat' to the attribute being validated. For example, if 'password' is |
||
54 | * being validated, then the attribute to be compared would be 'password_repeat'. |
||
55 | * @see compareValue |
||
56 | */ |
||
57 | public $compareAttribute; |
||
58 | /** |
||
59 | * @var mixed the constant value to be compared with. When both this property |
||
60 | * and [[compareAttribute]] are set, this property takes precedence. |
||
61 | * @see compareAttribute |
||
62 | */ |
||
63 | public $compareValue; |
||
64 | /** |
||
65 | * @var string the type of the values being compared. The follow types are supported: |
||
66 | * |
||
67 | * - [[TYPE_STRING|string]]: the values are being compared as strings. No conversion will be done before comparison. |
||
68 | * - [[TYPE_NUMBER|number]]: the values are being compared as numbers. String values will be converted into numbers before comparison. |
||
69 | */ |
||
70 | public $type = self::TYPE_STRING; |
||
71 | /** |
||
72 | * @var string the operator for comparison. The following operators are supported: |
||
73 | * |
||
74 | * - `==`: check if two values are equal. The comparison is done is non-strict mode. |
||
75 | * - `===`: check if two values are equal. The comparison is done is strict mode. |
||
76 | * - `!=`: check if two values are NOT equal. The comparison is done is non-strict mode. |
||
77 | * - `!==`: check if two values are NOT equal. The comparison is done is strict mode. |
||
78 | * - `>`: check if value being validated is greater than the value being compared with. |
||
79 | * - `>=`: check if value being validated is greater than or equal to the value being compared with. |
||
80 | * - `<`: check if value being validated is less than the value being compared with. |
||
81 | * - `<=`: check if value being validated is less than or equal to the value being compared with. |
||
82 | * |
||
83 | * When you want to compare numbers, make sure to also set [[type]] to `number`. |
||
84 | */ |
||
85 | public $operator = '=='; |
||
86 | /** |
||
87 | * @var string the user-defined error message. It may contain the following placeholders which |
||
88 | * will be replaced accordingly by the validator: |
||
89 | * |
||
90 | * - `{attribute}`: the label of the attribute being validated |
||
91 | * - `{value}`: the value of the attribute being validated |
||
92 | * - `{compareValue}`: the value or the attribute label to be compared with |
||
93 | * - `{compareAttribute}`: the label of the attribute to be compared with |
||
94 | * - `{compareValueOrAttribute}`: the value or the attribute label to be compared with |
||
95 | */ |
||
96 | public $message; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 7 | public function init() |
|
136 | |||
137 | /** |
||
138 | * @inheritdoc |
||
139 | */ |
||
140 | 4 | public function validateAttribute($model, $attribute) |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | 2 | protected function validateValue($value) |
|
183 | |||
184 | /** |
||
185 | * Compares two values with the specified operator. |
||
186 | * @param string $operator the comparison operator |
||
187 | * @param string $type the type of the values being compared |
||
188 | * @param mixed $value the value being compared |
||
189 | * @param mixed $compareValue another value being compared |
||
190 | * @return bool whether the comparison using the specified operator is true. |
||
191 | */ |
||
192 | 5 | protected function compareValues($operator, $type, $value, $compareValue) |
|
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | */ |
||
226 | public function clientValidateAttribute($model, $attribute, $view) |
||
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | public function getClientOptions($model, $attribute) |
||
267 | } |
||
268 |