首页 > 解决方案 > 验证码不接受 Yii

问题描述

我正确实施了验证码,如果我输入正确的值,它不会显示错误,但是在提交后,当我使用"$model->getErrors()". 它正在向我展示。

Array
(
    [verifyCode] => Array
        (
            [0] => The verification code is incorrect.
        )

)

行动:

 /**
     * {@inheritdoc}
     */
    public function actions() {
        $this->layout = '@app/views/layouts/login';
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
               // 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


public function actionIndex() {
        $session = Yii::$app->session;
        $session->set('step', 0);

        $this->layout = '@app/views/layouts/login';
        $model = new ForgotPassword();
        $post = $model->load(Yii::$app->request->post());
        if ($post) {
            $user_model = $model->findUserByEmail();
            if (isset($user_model)) {
                $session->set('step', 1);
                $model_forgotpassword = $model->findByEmail();
                if (isset($model_forgotpassword)) {
                    $model->isOtpExpired($model_forgotpassword->created_at);
                    FlashMessage::Success(flash::Messagelabel('EMAIL_OTP_ALREADY_SEND'));
                }
                $otp = \common\components\CSystemGenerated::password(6, 3, 3, 1);
                $model->otp = $otp;
                $model->otp_confirm = '';
                if ($model->validate()) {
                    if ($model->save()) {
                        FlashMessage::Success(flash::Messagelabel('EMAIL_SEND_WITH_OTP'));
                        return $this->render('index', [
                                    'model' => $model,
                        ]);
                    }
                }
                \common\components\CHelper::debug($model->getErrors());
            } else {
                $session->set('step', 0);
                FlashMessage::Warning(flash::Messagelabel('EMAIL_NOT_EXIST'));
            }
        }
        return $this->render('index', [
                    'model' => $model,
        ]);
    }

模型:

 ['verifyCode', 'captcha','captchaAction'=>'/auth/forgotpassword/captcha'],

看法:

<?= $form->field($model, 'verifyCode')->widget(\yii\captcha\Captcha::classname(), [
            'captchaAction'=>Url::to('/auth/forgotpassword/captcha'),
            // configure additional widget properties here
        ]) ?>

我在谷歌上搜索但我没有找到确切的问题。

标签: phpyii2captcha

解决方案


问题是你正在使用 $model->validate()$model->save()在一起。

$model->save()内部调用$model->validate()并调用$model->validate()两次,更改验证码。

只需删除额外if ($model->validate())的 .


推荐阅读