1: <?php
2: /**
3: * CRequiredValidator 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: * CRequiredValidator validates that the specified attribute does not have null or empty value.
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: * CRequiredValidator allows for the following placeholders to be specified:
18: * <ul>
19: * <li>{value}: replaced with the desired value {@link requiredValue}.</li>
20: * </ul>
21: *
22: * @author Qiang Xue <qiang.xue@gmail.com>
23: * @package system.validators
24: * @since 1.0
25: */
26: class CRequiredValidator extends CValidator
27: {
28: /**
29: * @var mixed the desired value that the attribute must have.
30: * If this is null, the validator will validate that the specified attribute does not have null or empty value.
31: * If this is set as a value that is not null, the validator will validate that
32: * the attribute has a value that is the same as this property value.
33: * Defaults to null.
34: */
35: public $requiredValue;
36: /**
37: * @var boolean whether the comparison to {@link requiredValue} is strict.
38: * When this is true, the attribute value and type must both match those of {@link requiredValue}.
39: * Defaults to false, meaning only the value needs to be matched.
40: * This property is only used when {@link requiredValue} is not null.
41: */
42: public $strict=false;
43: /**
44: * @var boolean whether the value should be trimmed with php trim() function when comparing strings.
45: * When set to false, the attribute value is not considered empty when it contains spaces.
46: * Defaults to true, meaning the value will be trimmed.
47: * @since 1.1.14
48: */
49: public $trim=true;
50: /**
51: * Validates the attribute of the object.
52: * If there is any error, the error message is added to the object.
53: * @param CModel $object the object being validated
54: * @param string $attribute the attribute being validated
55: */
56: protected function validateAttribute($object,$attribute)
57: {
58: $value=$object->$attribute;
59: if($this->requiredValue!==null)
60: {
61: if(!$this->strict && $value!=$this->requiredValue || $this->strict && $value!==$this->requiredValue)
62: {
63: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be {value}.',
64: array('{value}'=>$this->requiredValue));
65: $this->addError($object,$attribute,$message);
66: }
67: }
68: elseif($this->isEmpty($value,$this->trim))
69: {
70: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} cannot be blank.');
71: $this->addError($object,$attribute,$message);
72: }
73: }
74:
75: /**
76: * Returns the JavaScript needed for performing client-side validation.
77: * @param CModel $object the data object being validated
78: * @param string $attribute the name of the attribute to be validated.
79: * @return string the client-side validation script.
80: * @see CActiveForm::enableClientValidation
81: * @since 1.1.7
82: */
83: public function clientValidateAttribute($object,$attribute)
84: {
85: $message=$this->message;
86: if($this->requiredValue!==null)
87: {
88: if($message===null)
89: $message=Yii::t('yii','{attribute} must be {value}.');
90: $message=strtr($message, array(
91: '{value}'=>$this->requiredValue,
92: '{attribute}'=>$object->getAttributeLabel($attribute),
93: ));
94: return "
95: if(value!=" . CJSON::encode($this->requiredValue) . ") {
96: messages.push(".CJSON::encode($message).");
97: }
98: ";
99: }
100: else
101: {
102: if($message===null)
103: $message=Yii::t('yii','{attribute} cannot be blank.');
104: $message=strtr($message, array(
105: '{attribute}'=>$object->getAttributeLabel($attribute),
106: ));
107: if($this->trim)
108: $emptyCondition = "jQuery.trim(value)==''";
109: else
110: $emptyCondition = "value==''";
111: return "
112: if({$emptyCondition}) {
113: messages.push(".CJSON::encode($message).");
114: }
115: ";
116: }
117: }
118: }
119: