Php 验证码不符合Yii

Php 验证码不符合Yii,php,yii2,captcha,Php,Yii2,Captcha,我正确地实现了captcha,如果我输入正确的值,它不会显示错误,但在提交之后,当我使用$model->getErrors进行检查时。它在向我展示 Array ( [verifyCode] => Array ( [0] => The verification code is incorrect. ) ) 行动: 型号: 视图: 我在谷歌上搜索了一下,但没有找到确切的问题。问题是您使用的是$model->validat

我正确地实现了captcha,如果我输入正确的值,它不会显示错误,但在提交之后,当我使用$model->getErrors进行检查时。它在向我展示

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

)
行动:

型号:

视图:


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

问题是您使用的是$model->validate和$model->save

$model->save internal calls$model->validate并调用$model->validate两次,更改验证码

只要删除额外的if$model->validate即可

 /**
     * {@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
        ]) ?>