修复gvalid.CheckStruct自定义错误提示失效问题

This commit is contained in:
john
2018-08-17 16:47:55 +08:00
parent 21a758b3a2
commit c3aa421c98
3 changed files with 18 additions and 10 deletions

View File

@ -196,11 +196,19 @@ func (s *Server) patternToRegRule(rule string) (regrule string, names []string)
}
switch v[0] {
case ':':
regrule += `/([\w\.\-]+)`
names = append(names, v[1:])
if len(v) > 1 {
regrule += `/([\w\.\-]+)`
names = append(names, v[1:])
break
}
fallthrough
case '*':
regrule += `/{0,1}(.*)`
names = append(names, v[1:])
if len(v) > 1 {
regrule += `/{0,1}(.*)`
names = append(names, v[1:])
break
}
fallthrough
default:
// 特殊字符替换
v = gstr.ReplaceByMap(v, map[string]string{

View File

@ -453,17 +453,17 @@ func CheckStruct(st interface{}, rules map[string]string, msgs...map[string]inte
}
// 错误提示
if len(msg) > 0 {
ruleArray := strings.Split(match[1], "|")
msgArray := strings.Split(match[2], "|")
ruleArray := strings.Split(rule, "|")
msgArray := strings.Split(msg, "|")
for k, v := range ruleArray {
if len(msgArray[k]) == 0 {
continue
}
array := strings.Split(v, ":")
if _, ok := errMsgs[field.Name()]; !ok {
errMsgs[field.Name()] = make(map[string]string)
if _, ok := errMsgs[name]; !ok {
errMsgs[name] = make(map[string]string)
}
errMsgs[field.Name()].(map[string]string)[array[0]] = msgArray[k]
errMsgs[name].(map[string]string)[array[0]] = msgArray[k]
}
}
}

View File

@ -21,7 +21,7 @@ func main() {
// 使用结构体定义的校验规则和错误提示进行校验
gutil.Dump(gvalid.CheckStruct(user, nil))
return
// 自定义校验规则和错误提示,对定义的特定校验规则和错误提示进行覆盖
rules := map[string]string {
"Uid" : "required",