yii2验证码验证不起作用

时间:2018-07-11 06:06:51

标签: php yii2 captcha

我正在尝试在yii2应用程序的登录页面上添加验证码。 我已经尝试过一些教程,问题是验证码总是正确的,就像没有验证一样。

我已经尝试过了:

我的代码是:

LoginForm

public $username;
public $password;
public $rememberMe = true;
private $_user = false;
public $captcha; // add this varible to your model class.

    /**
     * @return array the validation rules.
     */
    public function rules() {
        return [
            // username and password are both required
            [['username', 'password','captcha'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            ['captcha', 'captcha','captchaAction'=>'/site/captcha'], // add this code to your rules.
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

SiteController

 public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,

            ],
        ];
    }

查看login.php

 <div class="form-goup">
           <?= $form->field($model, 'captcha')->widget(yii\captcha\Captcha::className(), '
['template' => '<div class="row"><div class="col-lg-3" style="margin-right:25px;">{image}</div><div class="col-lg-6">{input}</div></div>',
            ]); ?> 
    </div>

2 个答案:

答案 0 :(得分:0)

您的代码有效,但是您有语法错误,一个字母(这只是修饰性的)和一个多余的'完全错误:

<div class="form-group">
    <?= $form->field($model, 'captcha')->widget(yii\captcha\Captcha::className(), ['template' => '<div class="row"><div class="col-lg-3" style="margin-right:25px;">{image}</div><div class="col-lg-6">{input}</div></div>',]); ?> 
</div>

应该在提交按钮 <?php ActiveForm::end();?>

之前查看

答案 1 :(得分:0)

我知道这个话题很旧,但是可能是您的网址规则所致。您是否尝试添加:

xhr

在您的配置文件中,网址部分