验证码不参与Yii

时间:2018-12-03 06:12:26

标签: php yii2 captcha

我正确实现了验证码,如果我输入正确的值,则不会显示任何错误,但是在提交后,当我使用"$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上进行了搜索,但没有找到确切的问题。

1 个答案:

答案 0 :(得分:1)

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

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

只需删除其他if ($model->validate())