1: <?php
2: /**
3: * CRangeValidator 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: * CRangeValidator validates that the attribute value is among the list (specified via {@link range}).
13: * You may invert the validation logic with help of the {@link not} property (available since 1.1.5).
14: *
15: * @author Qiang Xue <qiang.xue@gmail.com>
16: * @package system.validators
17: * @since 1.0
18: */
19: class CRangeValidator extends CValidator
20: {
21: /**
22: * @var array list of valid values that the attribute value should be among
23: */
24: public $range;
25: /**
26: * @var boolean whether the comparison is strict (both type and value must be the same)
27: */
28: public $strict=false;
29: /**
30: * @var boolean whether the attribute value can be null or empty. Defaults to true,
31: * meaning that if the attribute is empty, it is considered valid.
32: */
33: public $allowEmpty=true;
34: /**
35: * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
36: * the attribute value should NOT be among the list of values defined via {@link range}.
37: * @since 1.1.5
38: **/
39: public $not=false;
40:
41: /**
42: * Validates the attribute of the object.
43: * If there is any error, the error message is added to the object.
44: * @param CModel $object the object being validated
45: * @param string $attribute the attribute being validated
46: * @throws CException if given {@link range} is not an array
47: */
48: protected function validateAttribute($object,$attribute)
49: {
50: $value=$object->$attribute;
51: if($this->allowEmpty && $this->isEmpty($value))
52: return;
53: if(!is_array($this->range))
54: throw new CException(Yii::t('yii','The "range" property must be specified with a list of values.'));
55: $result = false;
56: if($this->strict)
57: $result=in_array($value,$this->range,true);
58: else
59: {
60: foreach($this->range as $r)
61: {
62: $result = $r === '' || $value === '' ? $r === $value : $r == $value;
63: if($result)
64: break;
65: }
66: }
67: if(!$this->not && !$result)
68: {
69: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is not in the list.');
70: $this->addError($object,$attribute,$message);
71: }
72: elseif($this->not && $result)
73: {
74: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is in the list.');
75: $this->addError($object,$attribute,$message);
76: }
77: }
78:
79: /**
80: * Returns the JavaScript needed for performing client-side validation.
81: * @param CModel $object the data object being validated
82: * @param string $attribute the name of the attribute to be validated.
83: * @throws CException if given {@link range} is not an array
84: * @return string the client-side validation script.
85: * @see CActiveForm::enableClientValidation
86: * @since 1.1.7
87: */
88: public function clientValidateAttribute($object,$attribute)
89: {
90: if(!is_array($this->range))
91: throw new CException(Yii::t('yii','The "range" property must be specified with a list of values.'));
92:
93: if(($message=$this->message)===null)
94: $message=$this->not ? Yii::t('yii','{attribute} is in the list.') : Yii::t('yii','{attribute} is not in the list.');
95: $message=strtr($message,array(
96: '{attribute}'=>$object->getAttributeLabel($attribute),
97: ));
98:
99: $range=array();
100: foreach($this->range as $value)
101: $range[]=(string)$value;
102: $range=CJSON::encode($range);
103:
104: return "
105: if(".($this->allowEmpty ? "jQuery.trim(value)!='' && " : '').($this->not ? "jQuery.inArray(value, $range)>=0" : "jQuery.inArray(value, $range)<0").") {
106: messages.push(".CJSON::encode($message).");
107: }
108: ";
109: }
110: }