Validation code for YII Framework 2.0

Source: Internet
Author: User

Recently read a Yii tutorial video, is in accordance with the version of 1.1, I want to use Yii2.0 framework also refer to his study development, the results found a lot of different, now talk about the verification code thing

First add an action to do verification code image display, the actual thing called the Captchaactive class

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

In the template file refer to the use of the following, specific or more read the document

Echo Captcha::widget ([' name ' = ' captchaimg ', ' captchaaction ' = ' login/captcha ', ' Options ' =>[' style ' = ' Cursor:pointer; ', ' id ' = ' captchaimg '], ' template ' = ' {image} ');?>

where ' captchaaction ' = ' Login/captcha ' is to be noted, his default is Site/captcha but if done under a module, it will automatically add a module prefix, such as my admin\, has not been shown at the beginning, Later found to be rote problem, but do not know where to change, look at the document for a long time.

And finally the validation,

In the model class of the Rules method, [' Captcha ', ' captcha ', ' message ' = ' captcha error ', ' captchaaction ' = ' admin/login/captcha '],

where ' captchaaction ' and ' admin/login/captcha ' also need to be set, the default is Site/captcha and here to absolute path of all, it does not automatically add module ID part.

The last one is validation.

$model $_post [' LoginForm ']; $model->validate ());

Of course, you can also get error messages for validation results.

Add in Template

Echo Html::error ($model, ' captcha ');?>

OK, more details of things to continue groping, and the 1.1 version of the different places quite a lot of. But it's familiar. The main document is in English, slightly creating a bit of a barrier.

Validation code for YII Framework 2.0

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.