dd

yii 自定义增加客户端验证

jerry Yii 2015年08月23日 收藏
<?php
class OwnValidate extends CValidator
{
    /**
     * 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)
    {
        if(time() >= $value=$object->$attribute){
            $this->addError($object,$attribute,'过期时间不能小于当前时间!');
        }
    }
        /**
     * 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
     */
    public function clientValidateAttribute($object,$attribute)
    {
        $condition = "((new Date()).getTime() >= (new Date(value).getTime()))";
        return "
        if(".$condition.") {
            messages.push(".CJSON::encode('过期时间不能小于当前时间!').");
        }
        ";
    }
}
?>
dd