2015-09-26 23:03:06 4847次浏览 1条回答 0 悬赏 10 金钱

验证码正常显示,点击也能更换,dubgger里查session里也看到了数据,post过来的数据有的,然而就是显示所填的验证码不正确,

查了一下确实是$this->validate(),验证的时候不通过,验证码的比较,应该是session里存的和post过来的一样那就对了哇?

然后我该怎么查原因??

yii2学的挺累的,附一下代码

/*  \vendor\dektrium\yii2-user\models\LoginForm.php   */
public $captcha;
public function rules()//作者的rules格式不和yii2标准的写法一样,为嘛?
{
return [
'requiredFields' => [['login', 'password','captcha'], 'required'],
'captchalPattern'=>['captcha', 'captcha'],
'loginTrim' => ['login', 'trim'],
. . . .


/* \vendor\dektrium\yii2-user\controllers\SecurityController.php*/

public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
['allow' => true, 'actions' => ['login', 'auth','captcha'], 'roles' => ['?']],//没这个验证码不显示
['allow' => true, 'actions' => ['login', 'auth', 'logout'], 'roles' => ['@']],
]]];
}

/** @inheritdoc */
public function actions()
{
    return [
         'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                             'minLength' => 4,
                             'maxLength' => 4
            ],
.....
    ];
}


/*  \vendor\dektrium\yii2-user\views\security\login.php  */
use yii\captcha\Captcha;
. . . .
<?= $form->field($model, 'captcha')->widget(Captcha::className(), [
'captchaAction' => ['/user/security/captcha']
]) ?>
. . . .

  • 回答于 2015-09-27 08:59 举报

    Model的rules()中的captcha规则中的captchaAction选项和View中widget的captchaAction选项要一致。都是指向控制器中的actions()函数中引入的captcha。没有指定,框架使用默认值。

    你可以按这几个步骤定位问题:

    • 检查一下你的页面是否有验证码显示,如果没有说明widget找不到captchaAction(没有指定时使用框架默认)。
    • 如果有,看页面源码的src路径,是否和你的controller路由(看你的网址)一致:
      <img id="signupform-verifycode-image" src="/admin/auth/captcha?v=56073d7b671c6" alt="">
    • 以上这个一致不是强求的,src的路径可以使用其他位置(路由)的captcha。
    • 最后,你的validate()是用model规则中的captchaAction去验证的(没有指定时使用框架默认),和view的不一致肯定失效。

    另外,你的model规则的写法,建议用框架的guide所说的格式,以下Model的rules()供参考:

    class SignupForm extends Model
    {
        public $first_name;
        public $last_name;
        public $username;
        public $email;
        public $password;
        public $password2;
        public $verifyCode;
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                #username, 用户名改成系统自动产生的数字,基数为YYYYMM00001
                #first_name, last_name
                [['first_name','last_name'], 'required'],
                [['first_name','last_name'], 'string', 'min' => 1, 'max' => 32],
                #email
                ['email', 'filter', 'filter' => 'trim'],
                ['email', 'required'],
                ['email', 'email'],
                ['email', 'unique', 'targetClass' => '\app\modules\admin\models\User', 'message' => 'This email address has already been taken.'],
                #password
                ['password', 'required'],
                ['password', 'string', 'min' => 6],
                ['password2', 'compare', 'compareAttribute' => 'password', 'message'=>'Password repeat is inconsistent with password.'],
                #captcha
                ['verifyCode', 'captcha', 'captchaAction'=>'/admin/auth/captcha'],
            ];
        }
    
    1 条回复
    回复于 2015-09-28 22:46 回复

    谢谢您的答复,按你的思路,我也去走了一遍,没错的。自己新装了yii2和yii2-user,始终是一样的错误,用yii2自带的loginfrom模型,就没错的,打印了yii\captcha\CaptchaAction 的 validate,session和post过来数据节拍对不上。问了作者,一口咬定是没问题的,关闭了我的问题。哎,水平不行找不到原因

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
搞搞的传奇
见习主管

搞搞的传奇

注册时间:2015-02-24
最后登录:2020-06-04
在线时长:24小时46分
  • 粉丝8
  • 金钱5
  • 威望10
  • 积分345

热门问题