• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

yii2 model 验证规则rules 一条规则验证失败立即返回不继续验证其他字段

php 搞代码 4年前 (2022-01-22) 23次浏览 已收录 0个评论
文章目录[隐藏]

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 valid<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码@网*</div><pre>搞gaodaima代码

ation. * * 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(); }


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:yii2 model 验证规则rules 一条规则验证失败立即返回不继续验证其他字段

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址