1: <?php
2: /**
3: * CBooleanValidator class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CBooleanValidator validates that the attribute value is either {@link trueValue} or {@link falseValue}.
13: *
14: * When using the {@link message} property to define a custom error message, the message
15: * may contain additional placeholders that will be replaced with the actual content. In addition
16: * to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
17: * CBooleanValidator allows for the following placeholders to be specified:
18: * <ul>
19: * <li>{true}: replaced with value representing the true status {@link trueValue}.</li>
20: * <li>{false}: replaced with value representing the false status {@link falseValue}.</li>
21: * </ul>
22: *
23: * @author Qiang Xue <qiang.xue@gmail.com>
24: * @package system.validators
25: */
26: class CBooleanValidator extends CValidator
27: {
28: /**
29: * @var mixed the value representing true status. Defaults to '1'.
30: */
31: public $trueValue='1';
32: /**
33: * @var mixed the value representing false status. Defaults to '0'.
34: */
35: public $falseValue='0';
36: /**
37: * @var boolean whether the comparison to {@link trueValue} and {@link falseValue} is strict.
38: * When this is true, the attribute value and type must both match those of {@link trueValue} or {@link falseValue}.
39: * Defaults to false, meaning only the value needs to be matched.
40: */
41: public $strict=false;
42: /**
43: * @var boolean whether the attribute value can be null or empty. Defaults to true,
44: * meaning that if the attribute is empty, it is considered valid.
45: */
46: public $allowEmpty=true;
47:
48: /**
49: * Validates the attribute of the object.
50: * If there is any error, the error message is added to the object.
51: * @param CModel $object the object being validated
52: * @param string $attribute the attribute being validated
53: */
54: protected function validateAttribute($object,$attribute)
55: {
56: $value=$object->$attribute;
57: if($this->allowEmpty && $this->isEmpty($value))
58: return;
59: if(!$this->strict && $value!=$this->trueValue && $value!=$this->falseValue
60: || $this->strict && $value!==$this->trueValue && $value!==$this->falseValue)
61: {
62: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be either {true} or {false}.');
63: $this->addError($object,$attribute,$message,array(
64: '{true}'=>$this->trueValue,
65: '{false}'=>$this->falseValue,
66: ));
67: }
68: }
69:
70: /**
71: * Returns the JavaScript needed for performing client-side validation.
72: * @param CModel $object the data object being validated
73: * @param string $attribute the name of the attribute to be validated.
74: * @return string the client-side validation script.
75: * @see CActiveForm::enableClientValidation
76: * @since 1.1.7
77: */
78: public function clientValidateAttribute($object,$attribute)
79: {
80: $message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} must be either {true} or {false}.');
81: $message=strtr($message, array(
82: '{attribute}'=>$object->getAttributeLabel($attribute),
83: '{true}'=>$this->trueValue,
84: '{false}'=>$this->falseValue,
85: ));
86: return "
87: if(".($this->allowEmpty ? "jQuery.trim(value)!='' && " : '')."value!=".CJSON::encode($this->trueValue)." && value!=".CJSON::encode($this->falseValue).") {
88: messages.push(".CJSON::encode($message).");
89: }
90: ";
91: }
92: }
93: