Yii2model verification rule rules: one rule fails to be verified. immediate return does not continue to verify other fields

Source: Internet
Author: User
Yii2model verification rule rules: a rule verification failure immediately returns not to continue to verify other fields Mode: rules (); {code ...} if username is null, an error is returned immediately. password and age are not verified. Similarly, if age is empty, other fields will not be verified. if yii2 is not known, this... yii2 model verification rule rules, a rule verification failure will be returned immediately. if other fields are not verified

Mode: rules ();

public function rules(){     [['username', 'password'], 'required'],     ['age', 'required'],     // ......}

If username is null, an error is returned immediately. password and age are not verified. Similarly, if age is empty, other fields are not verified.

I don't know if yii2 has such configuration?

Reply content:

Yii2 model verification rule rules: one rule fails to be verified. immediate return does not continue to verify other fields

Mode: rules ();

public function rules(){     [['username', 'password'], 'required'],     ['age', 'required'],     // ......}

If username is null, an error is returned immediately. password and age are not verified. Similarly, if age is empty, other fields are not verified.

I don't know if yii2 has such configuration?

After verification, tell you which fields have not passed verification. For more information, see the code.

/*** Performs the data validation. ** This method executes the validation rules applicable to the current [[scenario]. * The following criteria are used to determine whether a rule is currently applicable: **-the rule must be associated with the attributes relevant to the current scenario; *-the rules must be valid tive for the current scenario. ** This method will call [[beforeValidate ()] And [[afterValidate ()] before and * after the actual validation, respectively. if [[beforeValidate ()] returns false, * the validation will be canceled and [[afterValidate ()] will not be called. ** Errors found during the validation can be retrieved via [[getErrors ()], * [[getFirstErrors ()] and [[getFirstError ()]. ** @ param array $ attributeNames list of attribute names that shocould be vali Dated. * If this parameter is empty, it means any attribute listed in the applicable * validation rules shoshould be validated. * @ param boolean $ clearErrors whether to call [[clearErrors ()] before validation * @ return boolean whether the validation is successful without any error. * @ throws InvalidParamException if the current scenario is unknown. */public function validate ($ attributeNa Mes = null, $ clearErrors = true) {if ($ clearErrors) {$ this-> clearErrors ();} if (! $ This-> beforeValidate () {return false;} $ scenarios = $ this-> scenarios (); $ scenario = $ this-> getScenario (); if (! Isset ($ scenarios [$ scenario]) {throw new InvalidParamException ("Unknown scenario: $ scenario");} if ($ attributeNames = null) {$ attributeNames = $ this-> activeAttributes ();} // note this foreach ($ this-> getActiveValidators () as $ validator) {$ validator-> validateAttributes ($ this, $ attributeNames);} $ this-> afterValidate (); return! $ This-> hasErrors ();}

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.