Detailed steps for adding a verification code to yii2-White Wolf stack

Source: Internet
Author: User
Yii2 detailed steps to add verification code-White Wolf stack author: White Wolf Source: Yellow.

I thought the yii2 framework verification code was comprehensive. I tried Baidu and google. most of the tutorials were not completely scattered. I thought I would write a verification code tutorial with the complete steps.

We assume that you need to add a verification code to log on to the site/login form.

1. added captcha settings for the actions method of siteController

public function actions() {     return [         'captcha' => [             'class' => 'yii\captcha\CaptchaAction',             'maxLength' => 4,             'minLength' => 4         ],     ]; }

We have simply set the number of digits of the verification code. some friends are curious about the configuration items. you can view the file vendor \ yiisoft \ yii2 \ captcha, including the background color of the verification code, font files and other settings can be found here.

2. configure siteController.

public function behaviors() {     return [         'access' => [             'class' => AccessControl::className(),             'rules' => [                 [                     'actions' => ['login', 'error', 'captcha'],                     'allow' => true,                 ],             ],         ]; }

Add the captcha method to access the actions of the access rules.

3. let's look at the view layer and add the verification code input.

use yii\captcha\Captcha; 
 field($model, 'verifyCode')->widget(Captcha::className(), [     'template' => '

{image}

{input}

', ]) ?>

4. we still need to add verification rules for verification codes.

We use LoginForm here, so we need to modify the LoginForm file.

Class LoginForm extends Model {//...... public $ verifyCode; public function rules () {return [//...... ['verifycode', 'captcha '],];} public function attributeLabels () {return ['verifycode' => '', // verification code name, set according to your preferences];} // defines the verifyCode attribute // the display name is defined in the verifyCode rule added validation // label

5. in step 4, the verification code is displayed normally after the verification code is configured. If rbac permission control is set in your background, I am afraid you still need to add/site/captcha for access to the as accss in config.

6. check the effect.

7. some people asked why the verification code is not refreshed on the page. I personally think it is not important to refresh it. the verification code is refreshed only when you enter the incorrect verification code page. If you have to refresh the page verification code and refresh it, try a simple method.

$ ('Verification code object'). click ();

That is, when the page is refreshed, click the verification code again to force the refresh.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.