model中的rules方法


验证器的一个实例:


public function rules() {
        return array(
            array('hall_id, guest_mobile, submit_time', 'required'),
            array('hall_id, submit_time, save_type', 'numerical', 'integerOnly' => true),
            array('hall_id, guest_mobile', 'length', 'max' => 11),
            array('guest_mobile', 'match', 'pattern' =>'/^(1[358][0-9]{9})|(15[89][0-9]{8})$/'),
            array('submit_time', 'length', 'max' => 10),
            array('password_repeat', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致', 'on' => array('register','phoneRegister','resetPwd','updatePwd')),
            array('email,user_name', 'unique','message'=>'已被注册'),
            array('verifyCode', 'ruleVerify', 'on' => 'phoneRegister'),
            array('id, hall_id, guest_mobile, submit_time, save_type', 'safe', 'on' => 'search'),
        );
    }


    public function ruleVerify() {
        $criteria = new CDbCriteria();
        $criteria ->compare("verify_code", $this->verifyCode);
        $criteria ->compare("account", $this->mobile_phone);
        $model = UserVerify::model()->find($criteria);
        if(empty($model) || $model->is_active==UserVerify::IS_ACTIVE_YES || $model->expire_time<time())
            $this->addError('verifyCode', '验证码不正确或已失效');
    }




你可能感兴趣的:(rules,yii,验证器)