Yii2 method to add verification code in modules

小云云
Release: 2023-03-20 10:20:02
Original
1381 people have browsed it

I recently played with the verification code part of Yii2, and the normal logic can be solved. There are no problems with the examples on the Internet. The key problematic part is when using it in the module. I will share it with everyone. Read on. You can go and see how it is used under normal circumstances before. This article mainly introduces to you how to add verification codes in modules in Yii2. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

The code of the controller part is similar to the one on the Internet


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, //设置字符偏移量 有效果
    ],
  ];
}
Copy after login

The code of the model part [Here are the things to pay attention to]


public function rules()
{
  return [
    ['username', 'required', 'message' => '登录账号不能为空'],
    ['password', 'required', 'message' => '登录密码不能为空'],
    ['verifyCode', 'required', 'message' => '验证码不能为空'],
    ['verifyCode', 'captcha', 'captchaAction' => 'admin/default/captcha', 'message' => '验证码输入错误'],
    ['rememberMe', 'boolean'],
    ['password', 'validatePassword'],
  ];
}
Copy after login

verifyCode in rules needs to add a value corresponding to captchaAction, otherwise the verification code verification will not pass, and the number of the verification code will not change. The reason should be that it is used by default. The code of the

view part caused by site/captcha [Due to the mixed layout of php and html, I can’t stand the chaotic layout of the page style, so I try to take out the parameter configuration part]


$captchaConfig = [
  'name' => 'captchaimg',
  'captchaAction' => ['/admin/default/captcha'],
  &#39;template&#39; => &#39;<p class="form-group"><p>{image}</p></p>&#39;,
  &#39;imageOptions&#39; => [
    &#39;id&#39; => &#39;captchaimg&#39;,
    &#39;title&#39; => &#39;换一个&#39;,
    &#39;alt&#39; => &#39;换一个&#39;,
    &#39;style&#39; => &#39;cursor:pointer;margin-left:25px;&#39;,
  ],
];
Copy after login


<?=Captcha::widget($captchaConfig);?>
Copy after login

Related recommendations:

Detailed explanation of modules instances of vuex2.0

Yii2 Detailed explanation of how to add verification codes in modules

Directory under modules in the yaf framework, configure the second-level domain name

The above is the detailed content of Yii2 method to add verification code in modules. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!