specialnot 2015-09-23 13:02:10 5852次浏览 0条评论 7 3 0

model中

'captchaAction'=>'/user/captcha' 很重要,表示验证码进行验证控制器位置,默认是 /site/captcha

['verifyCode', 'captcha','message'=>'验证码不正确','captchaAction'=>'/user/captcha','on'=>['change_username','change_password','change_email','change_mobile']],

controller中

在 userController中加入如下代码[一定要和model 验证中的 captchaAction 相对应]

public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' =>  [
            'class' => 'yii\captcha\CaptchaAction',
            'height' => 50,
            'width' => 80,
            'minLength' => 4,
            'maxLength' => 4
        ],
    ];
}

view中

captchaAction 很重要,表示获取验证码的控制器,默认是 /site/captcha

<?= $form->field($model,'verifyCode')->widget(
    yii\captcha\Captcha::className(),
    [
        'captchaAction'=>'/user/captcha',
        'template' => '<div class="row"><div class="col-md-3">{image}</div><div class="col-md-3">{input}</div></div>',
        'imageOptions'=>['alt'=>'图片无法加载','title'=>'点击换图', 'style'=>'cursor:pointer'],
    ]
);?>

注意:一定要保证 model 中的 captchaAction 和 view 中的 captchaAction保持一致,不然会验证错误。

觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册