You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
而其英文注释为:
Required unless all given field and its value are equal.
而其代码实现为:
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.//// This Source Code Form is subject to the terms of the MIT License.// If a copy of the MIT was not distributed with this file,// You can obtain one at https://github.com/gogf/gf.package builtin
import (
"errors""strings""github.com/gogf/gf/v2/util/gconv""github.com/gogf/gf/v2/util/gutil"
)
// RuleRequiredIf implements `required-if` rule:// Required unless all given field and its value are equal.//// Format: required-if:field,value,...// Example: required-if: id,1,age,18typeRuleRequiredIfstruct{}
funcinit() {
Register(RuleRequiredIf{})
}
func (rRuleRequiredIf) Name() string {
return"required-if"
}
func (rRuleRequiredIf) Message() string {
return"The {field} field is required"
}
func (rRuleRequiredIf) Run(inRunInput) error {
var (
required=falsearray=strings.Split(in.RulePattern, ",")
foundValueinterface{}
)
// It supports multiple field and value pairs.iflen(array)%2==0 {
fori:=0; i<len(array); {
tk:=array[i]
tv:=array[i+1]
_, foundValue=gutil.MapPossibleItemByKey(in.Data.Map(), tk)
ifin.Option.CaseInsensitive {
required=strings.EqualFold(tv, gconv.String(foundValue))
} else {
required=strings.Compare(tv, gconv.String(foundValue)) ==0
}
ifrequired {
break
}
i+=2
}
}
ifrequired&&isRequiredEmpty(in.Value.Val()) {
returnerrors.New(in.Message)
}
returnnil
}
Hello @cococolanosugar. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @cococolanosugar。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。
required-if的困惑
关于required-if校验规则:
中文文档中说明为:
必需参数(当任意所给定字段值与所给值相等时,即:当field字段的值为value时,当前验证字段为必须参数)。多个字段以,号分隔。
而其英文注释为:
Required unless all given field and its value are equal.
而其代码实现为:
综合看起来,代码实现跟中文说明相符,而与英文注释不符。
同理required-unless也有相应confuse!
建议:
将required-if拆分为多个更易于理解的规则,例如:
required-if-all-eq: 如果所有条件符合,则该字段必须;
required-if-any-eq: 如果任一条件符合,则该字段必须;
The text was updated successfully, but these errors were encountered: