mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 19:20:57 +00:00
Merge pull request #4225 from HITWHTigerLiu/develop-2.0
Empty field in validator.Error when label struct tag is not declared #4222
This commit is contained in:
commit
df043f22fc
@ -269,6 +269,11 @@ func (v *Validation) apply(chk Validator, obj interface{}) *Result {
|
||||
Field := ""
|
||||
Label := ""
|
||||
parts := strings.Split(key, ".")
|
||||
if len(parts) == 2 {
|
||||
Field = parts[0]
|
||||
Name = parts[1]
|
||||
Label = Field
|
||||
}
|
||||
if len(parts) == 3 {
|
||||
Field = parts[0]
|
||||
Name = parts[1]
|
||||
|
@ -607,3 +607,28 @@ func TestCanSkipAlso(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFieldNoEmpty(t *testing.T) {
|
||||
type User struct {
|
||||
Name string `json:"name" valid:"Match(/^[a-zA-Z][a-zA-Z0-9._-]{0,31}$/)"`
|
||||
}
|
||||
u := User{
|
||||
Name: "*",
|
||||
}
|
||||
|
||||
valid := Validation{}
|
||||
b, err := valid.Valid(u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b {
|
||||
t.Fatal("validation should be passed")
|
||||
}
|
||||
if len(valid.Errors) == 0 {
|
||||
t.Fatal("validation should be passed")
|
||||
}
|
||||
validErr := valid.Errors[0]
|
||||
if len(validErr.Field) == 0 {
|
||||
t.Fatal("validation should be passed")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user