123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * CRequiredValidator class file.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.yiiframework.com/
- * @copyright 2008-2013 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- /**
- * CRequiredValidator validates that the specified attribute does not have null or empty value.
- *
- * When using the {@link message} property to define a custom error message, the message
- * may contain additional placeholders that will be replaced with the actual content. In addition
- * to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
- * CRequiredValidator allows for the following placeholders to be specified:
- * <ul>
- * <li>{value}: replaced with the desired value {@link requiredValue}.</li>
- * </ul>
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @package system.validators
- * @since 1.0
- */
- class CRequiredValidator extends CValidator
- {
- /**
- * @var mixed the desired value that the attribute must have.
- * If this is null, the validator will validate that the specified attribute does not have null or empty value.
- * If this is set as a value that is not null, the validator will validate that
- * the attribute has a value that is the same as this property value.
- * Defaults to null.
- */
- public $requiredValue;
- /**
- * @var boolean whether the comparison to {@link requiredValue} is strict.
- * When this is true, the attribute value and type must both match those of {@link requiredValue}.
- * Defaults to false, meaning only the value needs to be matched.
- * This property is only used when {@link requiredValue} is not null.
- */
- public $strict=false;
- /**
- * @var boolean whether the value should be trimmed with php trim() function when comparing strings.
- * When set to false, the attribute value is not considered empty when it contains spaces.
- * Defaults to true, meaning the value will be trimmed.
- * @since 1.1.14
- */
- public $trim=true;
- /**
- * Validates the attribute of the object.
- * If there is any error, the error message is added to the object.
- * @param CModel $object the object being validated
- * @param string $attribute the attribute being validated
- */
- protected function validateAttribute($object,$attribute)
- {
- $value=$object->$attribute;
- if($this->requiredValue!==null)
- {
- if(!$this->strict && $value!=$this->requiredValue || $this->strict && $value!==$this->requiredValue)
- {
- $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be {value}.',
- array('{value}'=>$this->requiredValue));
- $this->addError($object,$attribute,$message);
- }
- }
- elseif($this->isEmpty($value,$this->trim))
- {
- $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} cannot be blank.');
- $this->addError($object,$attribute,$message);
- }
- }
- /**
- * Returns the JavaScript needed for performing client-side validation.
- * @param CModel $object the data object being validated
- * @param string $attribute the name of the attribute to be validated.
- * @return string the client-side validation script.
- * @see CActiveForm::enableClientValidation
- * @since 1.1.7
- */
- public function clientValidateAttribute($object,$attribute)
- {
- $message=$this->message;
- if($this->requiredValue!==null)
- {
- if($message===null)
- $message=Yii::t('yii','{attribute} must be {value}.');
- $message=strtr($message, array(
- '{value}'=>$this->requiredValue,
- '{attribute}'=>$object->getAttributeLabel($attribute),
- ));
- return "
- if(value!=" . CJSON::encode($this->requiredValue) . ") {
- messages.push(".CJSON::encode($message).");
- }
- ";
- }
- else
- {
- if($message===null)
- $message=Yii::t('yii','{attribute} cannot be blank.');
- $message=strtr($message, array(
- '{attribute}'=>$object->getAttributeLabel($attribute),
- ));
- if($this->trim)
- $emptyCondition = "jQuery.trim(value)==''";
- else
- $emptyCondition = "value==''";
- return "
- if({$emptyCondition}) {
- messages.push(".CJSON::encode($message).");
- }
- ";
- }
- }
- }
|