yii2model验证规则rules一条规则验证失败立即返回不继续验证其他字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了yii2model验证规则rules一条规则验证失败立即返回不继续验证其他字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2343字,纯文字阅读大概需要4分钟。
内容图文
yii2 model 验证规则rules 一条规则验证失败立即返回不继续验证其他字段Mode::rules();
public function rules() { [['username', 'password'], 'required'], ['age', 'required'], // ...... }
username 为空立即返回error ,不去验证password和age。同理age如果为空,也不会去验证其他字段
不知道yii2这块有没有这种配置?
回复内容:
yii2 model 验证规则rules 一条规则验证失败立即返回不继续验证其他字段
Mode::rules();
public function rules() { [['username', 'password'], 'required'], ['age', 'required'], // ...... }
username 为空立即返回error ,不去验证password和age。同理age如果为空,也不会去验证其他字段
不知道yii2这块有没有这种配置?
验证完,再告诉你哪些字段没有通过验证。具体看代码。
/** * 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(); }
内容总结
以上是互联网集市为您收集整理的yii2model验证规则rules一条规则验证失败立即返回不继续验证其他字段全部内容,希望文章能够帮你解决yii2model验证规则rules一条规则验证失败立即返回不继续验证其他字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。