The use of rules in a YII2.0 model

Source: Internet
Author: User

According to the official documentation, Http://www.yiichina.com/doc/guide/2.0/input-validation#standalone-validators needs to define the rules in the model

The Rules method specifies the scope of use, and the method is called every time the save is called.

Official documentation explains:

For each rule, you need to specify at least which attributes the rule applies to, and what the type of the rule is. You can specify one of the following rule types:

    • Core Validator nickname, such as  required , in , date , and so on. Please refer to the Core Validator section for a complete list of core validators. The name of a validation method in the

    • Model class, or an anonymous method. Please refer to the In-line Validator section for more information. The name of the

    • Validator class. Please refer to the Independent Validator section for more information.

A rule can be used to validate one or more model attributes, and an attribute can be validated by one or more rules. A rule can be applied to a particular scene (scenario), as long as the designation  on   Options. If you do not specify  on   option, the rule will fit in all scenarios.

when calling  validate ()   method, it will run the following specific verification steps:

    1. Checks the current [[Yii\base\] selected from the scene declared from the [[Yii\base\model::scenarios ()]] method Model::scenario| scene]] to determine which features need to be verified. These attributes are called activation attributes.

    2. Examines the rules that apply to the current [[yii\base\model::scenario| scene]] from the many rules declared from the [[Yii\base\model::rules ()]] method. This determines which rules need to be validated. These rules are called activation rules.

    3. Validates each activation rule with the activation attribute associated with it.

Based on the verification steps above, there is only an scenarios() activation attribute declared in the method, and it must also be associated with one or more rules() of the activation rules in the declaration itself before being validated.

For example: rules are defined in our backend user class

/**     *  validation rules      *  @return  string  Return validation Information      */    public function rules ()  {          return [             //fill in User name   password   Verification code   validation rules              [' uname ', ' Required ', ' message ' and ' user name cannot be null '],             [' upwd ', ' Required ', ' message ' = ' password cannot be empty '],             [' Verifycode ', ' Required ', ' message ' = ' Verification code cannot be empty '],             [                  ' Verifycode ',  ' captcha ',                  ' captchaaction ' = ' Login/captcha ',                  ' message ' + ' verification code is incorrect! '             ],          ];    }

We are right when we log in, if we edit the user information, save the words will not be successful, because there is no verification code in the editing state so it is a failure

Then we need to make the following corrections, specifying an on, which refers to the model, not the controller

/**     *  validation rules      *  @return  string  Return validation Information      */    public function rules ()  {          return [             //fill in User name   password   Verification code   validation rules              [' uname ', ' Required ', ' message ' + ' user name cannot be empty ',    ' on ' + ' login '],             [' upwd ', ' Required ', ' message ' = ' password cannot be empty ',    ' on ' = ' login '],            [' Verifycode ', ' Required ', ' message ' = ' Verification code cannot be null ',   ' on ' + ' login '],             [                  ' veRifycode ',  ' captcha ',                  ' captchaaction ' = ' Login/captcha ',                  ' message ' + ' verification code is incorrect! ',                           ' on ' = ' login '              ],         ];    }

So we don't run this rule when we edit the user in the background.

This article is from the "Chase Dream" blog, please be sure to keep this source http://dreameng.blog.51cto.com/1187899/1650019

The use of rules in a YII2.0 model

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.