Yii 不接受验证码

Captcha Not accpeting on 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
        ]) ?>

我在 google 上搜索过,但没有找到确切的问题。

问题是您同时使用了 $model->validate()$model->save()

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

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