内容简介:Yii2 如何在modules中添加验证码
最近玩了下Yii2的验证码部分,正常的逻辑都可以走通的,网上的例子也是没有问题的,关键有问题的部分是在module中使用的时候,分享给大家,往下看之前可以去看看正常情况下是如何使用的。
controller部分的代码,这里的跟网上的都类似
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 部分的代码【这里是需要注意的】
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的混排导致我无法忍受页面样式的混乱排版,所以尽量将参数配置部分拿出来】
$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;',
],
];
<?=Captcha::widget($captchaConfig);?>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 更安全!Mozilla 将为 Firefox 帐号添加“两步验证”的支持
- ubuntu 18.04 ssh 登录添加 Google Authenticator 两步验证
- 如何在 VuePress 中添加博客导流公众号:即输入验证码解锁全站文章
- FileZilla 客户端 3.43.0 发布,更新机制中添加了第二层身份验证
- RapPhp v2.1.3 更新:添加验证器和控制器前置方法等新特性
- RapPhp v2.1.3 更新:添加验证器和控制器前置方法等新特性
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Websites with Joomla!
H Graf / Packt Publishing / 2006-01-20 / USD 44.99
This book is a fast paced tutorial to creating a website using Joomla!. If you've never used Joomla!, or even any web content management system before, then this book will walk you through each step i......一起来看看 《Building Websites with Joomla!》 这本书的介绍吧!