Home >Backend Development >PHP Tutorial >yii2 model verification rules rules If a rule fails to be verified, it will be returned immediately without continuing to verify other fields.

yii2 model verification rules rules If a rule fails to be verified, it will be returned immediately without continuing to verify other fields.

ringa_lee
ringa_leeOriginal
2018-05-10 14:35:211416browse


yii2 model verification rules rules If a rule fails to be verified, it will be returned immediately without continuing to verify other fields.

Mode::rules();

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

}

If username is empty, error will be returned immediately without verifying password and age. In the same way, if age is empty, other fields will not be verified

I wonder if yii2 has such a configuration?

Reply content:

yii2 model verification rules rules If a rule fails to be verified, it will be returned immediately without continuing to verify other fields.

Mode::rules();

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

}

If username is empty, error will be returned immediately without verifying password and age. In the same way, if age is empty, other fields will not be verified

I wonder if yii2 has such a configuration?

After verification, we will tell you which fields did not pass verification. See the code for details.

/**
     * 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 effective 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 cancelled 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 should be validated.
     * If this parameter is 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 whether the validation is successful without any error.
     * @throws InvalidParamException if the current scenario is unknown.
     */
    public function validate($attributeNames = 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();
        }
        //注意这个foreach
        foreach ($this->getActiveValidators() as $validator) {
            $validator->validateAttributes($this, $attributeNames);
        }
        $this->afterValidate();

        return !$this->hasErrors();
    }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn