时间:2021-07-01 10:21:17 帮助过:15人阅读
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' => '<p class="form-group"><p>{image}</p></p>',
  'imageOptions' => [
    'id' => 'captchaimg',
    'title' => '换一个',
    'alt' => '换一个',
    'style' => 'cursor:pointer;margin-left:25px;',
  ],
];<?=Captcha::widget($captchaConfig);?>
相关推荐:
vuex2.0 之modules实例详解
Yii2 如何在modules中添加验证码的方法详解
yaf框架中modules下的目录,配置二级域名
以上就是Yii2在modules中添加验证码的方法的详细内容,更多请关注Gxl网其它相关文章!