Yii2如何在modules中添加验证码的方法
controller部分的代码,这里的跟网上的都类似
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction' ,
'fixedVerifyCode' => null,
'backColor' => 0x000000, //背景颜色
'maxLength' => 6, //最大显示个数
'minLength' => 5, //最少显示个数
'padding' => 5, //间距
'height' => 40, //高度
'width' => 130, //宽度
'foreColor' => 0xffffff, //字体颜色
'offset' => 4, //设置字符偏移量 有效果
],
];
}
|
model 部分的代码【这里是需要注意的】
1
2
3
4
5
6
7
8
9
10
11
|
public function rules()
{
return [
[ 'username' , 'required' , 'message' => '登录账号不能为空' ],
[ 'password' , 'required' , 'message' => '登录密码不能为空' ],
[ 'verifyCode' , 'required' , 'message' => '验证码不能为空' ],
[ 'verifyCode' , 'captcha' , 'captchaAction' => 'admin/default/captcha' , 'message' => '验证码输入错误' ],
[ 'rememberMe' , 'boolean' ],
[ 'password' , 'validatePassword' ],
];
}
|
rules中的verifyCode,需要加一个captchaAction对应的值,不然会出现验证码验证不通过,而且验证码的的数字也不会变化,原因应该是默认使用了site/captcha导致的
view部分的代码【由于php跟html的混排导致我无法忍受页面样式的混乱排版,所以尽量将参数配置部分拿出来】
1
2
3
4
5
6
7
8
9
10
11
|
$captchaConfig = [
'name' => 'captchaimg' ,
'captchaAction' => [ '/admin/default/captcha' ],
'template' => '<div class="form-group"><div>{image}</div></div>' ,
'imageOptions' => [
'id' => 'captchaimg' ,
'title' => '换一个' ,
'alt' => '换一个' ,
'style' => 'cursor:pointer;margin-left:25px;' ,
],
];
|
1
|
<?=Captcha::widget( $captchaConfig );?>
|
/php/27917.htmlwww.phpzy.comtrue/php/27917.htmlTechArticleYii2如何在modules中添加验证码的方法 controller部分的代码,这里的跟网上的都类似 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public function actions() { return [ 'captcha' = [ 'class' = 'yii\captcha\CaptchaAction' ,...
相关文章
- 暂无相关文章
今天推荐
PHP之友评论