1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-18 13:04:14 +00:00

Add function to set validation default messages

This commit is contained in:
miraclesu 2016-01-05 21:14:35 +08:00
parent f7ef4aa7e5
commit bef6bca397
3 changed files with 34 additions and 2 deletions

View File

@ -41,7 +41,7 @@ Direct Use:
}
}
// or use like this
if v := valid.Max(u.Age, 140); !v.Ok {
if v := valid.Max(u.Age, 140, "ageMax"); !v.Ok {
log.Println(v.Error.Key, v.Error.Message)
}
}

View File

@ -38,7 +38,7 @@
// }
// }
// // or use like this
// if v := valid.Max(u.Age, 140); !v.Ok {
// if v := valid.Max(u.Age, 140, "ageMax"); !v.Ok {
// log.Println(v.Error.Key, v.Error.Message)
// }
// }

View File

@ -46,6 +46,38 @@ var MessageTmpls = map[string]string{
"ZipCode": "Must be valid zipcode",
}
// set default messages
// if not set, the default messages are
// "Required": "Can not be empty",
// "Min": "Minimum is %d",
// "Max": "Maximum is %d",
// "Range": "Range is %d to %d",
// "MinSize": "Minimum size is %d",
// "MaxSize": "Maximum size is %d",
// "Length": "Required length is %d",
// "Alpha": "Must be valid alpha characters",
// "Numeric": "Must be valid numeric characters",
// "AlphaNumeric": "Must be valid alpha or numeric characters",
// "Match": "Must match %s",
// "NoMatch": "Must not match %s",
// "AlphaDash": "Must be valid alpha or numeric or dash(-_) characters",
// "Email": "Must be a valid email address",
// "IP": "Must be a valid ip address",
// "Base64": "Must be valid base64 characters",
// "Mobile": "Must be valid mobile number",
// "Tel": "Must be valid telephone number",
// "Phone": "Must be valid telephone or mobile phone number",
// "ZipCode": "Must be valid zipcode",
func SetDefaultMessage(msg map[string]string) {
if len(msg) == 0 {
return
}
for name := range msg {
MessageTmpls[name] = msg[name]
}
}
// Validator interface
type Validator interface {
IsSatisfied(interface{}) bool