谁一开始就是大神?

Yii2的Captcha使用Ajax进行验证出错[2.0]

D:\phpwork\news\controllers\SiteController.php

   public function actionRegister()
    {
        $model = new RegisterForm();
        if ($model->load(Yii::$app->request->post())) {
           if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                $result=\yii\widgets\ActiveForm::validate($model);
                return $result;
            }
            if($model->validate()){
                $error=$model->save();
                if($error) {
                    return $this->errorDisplay($error);
                }else{
                    Yii::$app->session->setFlash('success');
                    return $this->refresh();
                }
            }else{
                return $this->errorDisplay($model->getErrors());
            }
        }else{
            return $this->render('register', [
                'model' => $model,
            ]);
        }
    }

D:\phpwork\news\models\RegisterForm.php

            ['verifyCode', 'captcha', 'captchaAction'=>'site/captcha'],

报错信息:

The verification code is incorrect.

个人感觉是下面这段Ajax验证代码运行后,verifyCode就被自动刷新了:

           if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                $result=\yii\widgets\ActiveForm::validate($model);
                return $result;
            }

请问大神们是怎么解决的这个问题?谢谢

` use yii\captcha\CaptchaAction;
[‘verifyCode’, ‘captcha’,’message’=>’验证码不正确’],
而不是这么写的
[‘verifyCode’, ‘captcha’, ‘captchaAction’=>’site/captcha’],
我将rules的规则
[ 'coder','captcha', 'captchaAction'=>'coder/captcha','message'=>"{attribute}错误"]
给删除了,已经用ajax验证,就不进行再次验证了,直接提交表单的时候就可以通过了。

赞(0) 打赏
未经允许不得转载:菜鸟之家 » Yii2的Captcha使用Ajax进行验证出错[2.0]

评论 抢沙发

登录

找回密码

注册