YII2 Add verification code detailed steps yii2.0 Verification code yii Verification Code does not display YII2 custom validation

Source: Internet
Author: User
Author: White Wolf Source: Http://www.manks.top/article/yii2_captcha This article copyright belongs to the author, welcome reprint, but without the author consent must retain this paragraph statement, and in the article page obvious location gives the original text connection, otherwise reserves the right to pursue legal responsibility.

Would have thought YII2 framework verification code This piece is comprehensive, try Baidu Google a bit, most of the tutorials written fragmented not comprehensive, think of their own to write a full step verification code tutorial.

We assume that the Site/login form login requires an additional verification code.

1, Sitecontroller Controller of the actions method to increase the CAPTCHA settings

 Public function actions () {     return  [         ' captcha ' + [             ' class ' = ' = ' yii\captcha\ Captchaaction ',             ' maxLength ' = 4,             ' minLength ' = 4        ],    ];}

Above we simply set the number of digits of the verification code, there are small partners curious what configuration items, this you can view the file Vendor\yiisoft\yii2\captcha, including the captcha background color, font files and other settings can be found here.

2, Sitecontroller continue to configure.

 Public function behaviors () {     return  [         ' access ' + =             ' class ' = ' AccessControl: : ClassName (),             ' rules ' and                     ' = [' actions ' = [' login ', ' error ', ' Captcha '],                      True,                ],            ],        ];}

Add the Captcha method to the actions of access rules to access it.

3, we look at the view layer, add the verification code input.

   $form->field ($model, ' Verifycode ')->widget (Captcha::classname ( ), [     ' template ' = ' {image}{input} ',?>

4, this does not work, we also need to increase the verification code validation rules

We're using loginform here, so modify the LoginForm file

class extends Model {     //publicfunction  rules () {         return  [             //            [' Verifycode ', ' captcha '],        ];     }       Public function Attributelabels () {         return  [             //        ];      // defined Verifycode attribute//rules rule added validation//label defined its display name

5, to the fourth step basically configure the verification code will be normal display. If you set the RBAC permission control in the background, I'm afraid you still need to add/site/captcha to the as Accss in config to be accessible.

6, look at the effect is good.

7, a classmate asked why the page refresh verification code does not follow the refresh, I personally feel that the brush does not refresh is not important, when you lose the wrong Verification code page refresh when the verification code will be refreshed. If you have to refresh the page verification code, try a simple method implementation.

$ (' Captcha object '). Click ();

That is, when the page is refreshed, re-click on the Verification code to force the refresh.

The above describes the Yii2 add verification code detailed steps, including YII, verification code aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.