BooleanValidator.php 3.16 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 40 41 42 43 44 45
	/**
	 * Initializes the validator.
	 */
	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 51 52
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
53
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
54 55
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
56
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
57 58
	{
		$value = $object->$attribute;
Qiang Xue committed
59
		if (!$this->validateValue($value)) {
Qiang Xue committed
60
			$this->addError($object, $attribute, $this->message, array(
w  
Qiang Xue committed
61 62 63 64 65 66
				'{true}' => $this->trueValue,
				'{false}' => $this->falseValue,
			));
		}
	}

Qiang Xue committed
67 68 69 70 71
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
Qiang Xue committed
72 73
	public function validateValue($value)
	{
Qiang Xue committed
74 75
		return !$this->strict && ($value == $this->trueValue || $value == $this->falseValue)
			|| $this->strict && ($value === $this->trueValue || $value === $this->falseValue);
Qiang Xue committed
76 77
	}

w  
Qiang Xue committed
78 79
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
80
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
81
	 * @param string $attribute the name of the attribute to be validated.
82 83
	 * @param \yii\base\View $view the view object that is going to be used to render views or view files
	 * containing a model form with this validator applied.
w  
Qiang Xue committed
84 85
	 * @return string the client-side validation script.
	 */
86
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
87
	{
88
		$options = array(
Qiang Xue committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
			'trueValue' => $this->trueValue,
			'falseValue' => $this->falseValue,
			'message' => Html::encode(strtr($this->message, array(
				'{attribute}' => $object->getAttributeLabel($attribute),
				'{value}' => $object->$attribute,
				'{true}' => $this->trueValue,
				'{false}' => $this->falseValue,
			))),
		);
		if ($this->skipOnEmpty) {
			$options['skipOnEmpty'] = 1;
		}
		if ($this->strict) {
			$options['strict'] = 1;
		}

105
		ValidationAsset::register($view);
Qiang Xue committed
106
		return 'yii.validation.boolean(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
107 108
	}
}