BooleanValidator.php 2.23 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\helpers\Html;
Qiang Xue committed
12

w  
Qiang Xue committed
13
/**
w  
Qiang Xue committed
14 15 16 17
 * BooleanValidator checks if the attribute value is a boolean value.
 *
 * Possible boolean values can be configured via the [[trueValue]] and [[falseValue]] properties.
 * And the comparison can be either [[strict]] or not.
w  
Qiang Xue committed
18 19
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
20
 * @since 2.0
w  
Qiang Xue committed
21
 */
w  
Qiang Xue committed
22
class BooleanValidator extends Validator
w  
Qiang Xue committed
23 24 25 26 27 28 29 30 31 32
{
	/**
	 * @var mixed the value representing true status. Defaults to '1'.
	 */
	public $trueValue = '1';
	/**
	 * @var mixed the value representing false status. Defaults to '0'.
	 */
	public $falseValue = '0';
	/**
w  
Qiang Xue committed
33 34
	 * @var boolean whether the comparison to [[trueValue]] and [[falseValue]] is strict.
	 * When this is true, the attribute value and type must both match those of [[trueValue]] or [[falseValue]].
w  
Qiang Xue committed
35 36 37 38
	 * Defaults to false, meaning only the value needs to be matched.
	 */
	public $strict = false;

Qiang Xue committed
39
	/**
Qiang Xue committed
40
	 * @inheritdoc
Qiang Xue committed
41 42 43 44 45
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
46
			$this->message = Yii::t('yii', '{attribute} must be either "{true}" or "{false}".');
Qiang Xue committed
47 48 49
		}
	}

w  
Qiang Xue committed
50
	/**
Qiang Xue committed
51
	 * @inheritdoc
w  
Qiang Xue committed
52
	 */
Qiang Xue committed
53
	protected function validateValue($value)
w  
Qiang Xue committed
54
	{
Qiang Xue committed
55 56 57 58
		$valid = !$this->strict && ($value == $this->trueValue || $value == $this->falseValue)
			|| $this->strict && ($value === $this->trueValue || $value === $this->falseValue);
		if (!$valid) {
			return [$this->message, [
59 60
				'true' => $this->trueValue,
				'false' => $this->falseValue,
Qiang Xue committed
61 62 63
			]];
		} else {
			return null;
w  
Qiang Xue committed
64 65 66
		}
	}

Qiang Xue committed
67
	/**
Qiang Xue committed
68
	 * @inheritdoc
w  
Qiang Xue committed
69
	 */
70
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
71
	{
Alexander Makarov committed
72
		$options = [
Qiang Xue committed
73 74
			'trueValue' => $this->trueValue,
			'falseValue' => $this->falseValue,
Alexander Makarov committed
75
			'message' => Html::encode(strtr($this->message, [
Qiang Xue committed
76 77 78
				'{attribute}' => $object->getAttributeLabel($attribute),
				'{true}' => $this->trueValue,
				'{false}' => $this->falseValue,
Alexander Makarov committed
79 80
			])),
		];
Qiang Xue committed
81 82 83 84 85 86 87
		if ($this->skipOnEmpty) {
			$options['skipOnEmpty'] = 1;
		}
		if ($this->strict) {
			$options['strict'] = 1;
		}

88
		ValidationAsset::register($view);
Qiang Xue committed
89
		return 'yii.validation.boolean(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
90 91
	}
}