YII2 Model Validation Rule rules a rule validation failure immediately returns do not continue validating other fields

Source: Internet
Author: User
YII2 Model Validation Rule rules a rule validation failure immediately returns do not continue validating other fields

Mode::rules ();

Public Function Rules () {[     ' username ', ' password '], ' required '],     [' age ', ' required '],     //...}

Username returns error immediately, without verifying password and age. The same age if it's empty, it's not going to validate the other fields.

Do not know yii2 this piece have this kind of configuration?

Reply content:

YII2 Model Validation Rule rules a rule validation failure immediately returns do not continue validating other fields

Mode::rules ();

Public Function Rules () {[     ' username ', ' password '], ' required '],     [' age ', ' required '],     //...}

Username returns error immediately, without verifying password and age. The same age if it's empty, it's not going to validate the other fields.

Do not know yii2 this piece have this kind of configuration?

After the validation, I'll tell you which fields are not validated. See the code specifically.

/** * performs the data validation.     * * This method executes the validation rules applicable to the current [[scenario]]. * The following criteria was used to determine whether a rule was currently applicable: * *-the rule must be Asso     Ciated with the attributes relevant to the current scenario;     *-The rules must is effective for the current scenario. * * This method would call [[[Beforevalidate ()]] and [[Aftervalidate ()]] before and * after the actual validation, R Espectively.     If [[Beforevalidate ()]] returns FALSE, * the validation would be cancelled and [[Aftervalidate ()]] would not be called. * * Errors found during the validation can be retrieved via [[GetErrors ()]], * [[Getfirsterrors ()]] and [[GETF     Irsterror ()].     * * @param array $attributeNames List of attribute names that should be validated.   * If This parameter are empty, it means any attribute listed in the applicable * validation rules should be validated.  * @param Boolean $clearErrors whether to call [[Clearerrors ()]] before performing validation * @return Boolean Wheth     Er the validation is successful without any error.     * @throws Invalidparamexception If the current scenario is unknown. */Public Function validate ($attributeNames = null, $clearErrors = True) {if ($clearErrors) {$th        Is->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 foreach ($this->getactivevalidators () as $validator) {$validator->validate        Attributes ($this, $attributeNames);  } $this->aftervalidate ();      Return! $this->haserrors (); }
  • 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.