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
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* BooleanValidator checks if the attribute value is a boolean value. |
14
|
|
|
* |
15
|
|
|
* Possible boolean values can be configured via the [[trueValue]] and [[falseValue]] properties. |
16
|
|
|
* And the comparison can be either [[strict]] or not. |
17
|
|
|
* |
18
|
|
|
* @author Qiang Xue <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class BooleanValidator extends Validator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var mixed the value representing true status. Defaults to '1'. |
25
|
|
|
*/ |
26
|
|
|
public $trueValue = '1'; |
27
|
|
|
/** |
28
|
|
|
* @var mixed the value representing false status. Defaults to '0'. |
29
|
|
|
*/ |
30
|
|
|
public $falseValue = '0'; |
31
|
|
|
/** |
32
|
|
|
* @var bool whether the comparison to [[trueValue]] and [[falseValue]] is strict. |
33
|
|
|
* When this is true, the attribute value and type must both match those of [[trueValue]] or [[falseValue]]. |
34
|
|
|
* Defaults to false, meaning only the value needs to be matched. |
35
|
|
|
*/ |
36
|
|
|
public $strict = false; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
7 |
|
public function init() |
43
|
|
|
{ |
44
|
7 |
|
parent::init(); |
45
|
7 |
|
if ($this->message === null) { |
46
|
7 |
|
$this->message = Yii::t('yii', '{attribute} must be either "{true}" or "{false}".'); |
47
|
7 |
|
} |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
3 |
|
protected function validateValue($value) |
54
|
|
|
{ |
55
|
3 |
|
$valid = !$this->strict && ($value == $this->trueValue || $value == $this->falseValue) |
56
|
3 |
|
|| $this->strict && ($value === $this->trueValue || $value === $this->falseValue); |
57
|
|
|
|
58
|
3 |
|
if (!$valid) { |
59
|
3 |
|
return [$this->message, [ |
60
|
3 |
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
61
|
3 |
|
'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
62
|
3 |
|
]]; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
1 |
|
public function clientValidateAttribute($model, $attribute, $view) |
72
|
|
|
{ |
73
|
1 |
|
ValidationAsset::register($view); |
74
|
1 |
|
$options = $this->getClientOptions($model, $attribute); |
75
|
|
|
|
76
|
1 |
|
return 'yii.validation.boolean(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
1 |
|
public function getClientOptions($model, $attribute) |
83
|
|
|
{ |
84
|
|
|
$options = [ |
85
|
1 |
|
'trueValue' => $this->trueValue, |
86
|
1 |
|
'falseValue' => $this->falseValue, |
87
|
1 |
|
'message' => Yii::$app->getI18n()->format($this->message, [ |
88
|
1 |
|
'attribute' => $model->getAttributeLabel($attribute), |
89
|
1 |
|
'true' => $this->trueValue === true ? 'true' : $this->trueValue, |
90
|
1 |
|
'false' => $this->falseValue === false ? 'false' : $this->falseValue, |
91
|
1 |
|
], Yii::$app->language), |
92
|
1 |
|
]; |
93
|
1 |
|
if ($this->skipOnEmpty) { |
94
|
1 |
|
$options['skipOnEmpty'] = 1; |
95
|
1 |
|
} |
96
|
1 |
|
if ($this->strict) { |
97
|
1 |
|
$options['strict'] = 1; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
return $options; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|