BooleanValidator.php 2.92 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * BooleanValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13 14 15 16
 * 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
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
19
 * @since 2.0
w  
Qiang Xue committed
20
 */
w  
Qiang Xue committed
21
class BooleanValidator extends Validator
w  
Qiang Xue committed
22 23 24 25 26 27 28 29 30 31
{
	/**
	 * @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
32 33
	 * @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
34 35 36 37 38 39 40 41 42 43 44 45
	 * Defaults to false, meaning only the value needs to be matched.
	 */
	public $strict = false;
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
46
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
47 48
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
49
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
50 51
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
52
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
53
			return;
w  
Qiang Xue committed
54
		}
w  
Qiang Xue committed
55
		if (!$this->strict && $value != $this->trueValue && $value != $this->falseValue
w  
Qiang Xue committed
56
				|| $this->strict && $value !== $this->trueValue && $value !== $this->falseValue) {
Alexander Makarov committed
57
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be either {true} or {false}.');
w  
Qiang Xue committed
58 59 60 61 62 63 64 65 66
			$this->addError($object, $attribute, $message, array(
				'{true}' => $this->trueValue,
				'{false}' => $this->falseValue,
			));
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
67
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
68 69 70 71 72
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
Alexander Makarov committed
73
		$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be either {true} or {false}.');
w  
Qiang Xue committed
74 75
		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
76
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
77 78 79 80
			'{true}' => $this->trueValue,
			'{false}' => $this->falseValue,
		));
		return "
w  
Qiang Xue committed
81 82
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . "value!=" . json_encode($this->trueValue) . " && value!=" . json_encode($this->falseValue) . ") {
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
83 84 85 86
}
";
	}
}