yii 自定义增加客户端验证

jerry Yii 2015年08月23日 收藏
  1. <?php
  2. class OwnValidate extends CValidator
  3. {
  4.     /**
  5.      * Validates the attribute of the object.
  6.      * If there is any error, the error message is added to the object.
  7.      * @param CModel $object the object being validated
  8.      * @param string $attribute the attribute being validated
  9.      */
  10.     protected function validateAttribute($object,$attribute)
  11.     {
  12.         if(time() >= $value=$object->$attribute){
  13.             $this->addError($object,$attribute,'过期时间不能小于当前时间!');
  14.         }
  15.     }
  16.         /**
  17.      * Returns the JavaScript needed for performing client-side validation.
  18.      * @param CModel $object the data object being validated
  19.      * @param string $attribute the name of the attribute to be validated.
  20.      * @return string the client-side validation script.
  21.      * @see CActiveForm::enableClientValidation
  22.      */
  23.     public function clientValidateAttribute($object,$attribute)
  24.     {
  25.         $condition = "((new Date()).getTime() >= (new Date(value).getTime()))";
  26.         return "
  27.         if(".$condition.") {
  28.             messages.push(".CJSON::encode('过期时间不能小于当前时间!').");
  29.         }
  30.         ";
  31.     }
  32. }
  33. ?>