1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 19:50:56 +00:00

Update ParserForm for new form tag style

This commit is contained in:
miraclesu 2013-08-10 11:42:25 +08:00
parent 38f6f8eef7
commit e47b2b677d
2 changed files with 17 additions and 7 deletions

View File

@ -190,10 +190,15 @@ func ParseForm(form url.Values, obj interface{}) error {
continue continue
} }
fieldT := objT.Field(i) fieldT := objT.Field(i)
tag := fieldT.Tag.Get("form")
if len(tag) == 0 { tags := strings.Split(fieldT.Tag.Get("form"), ",")
var tag string
if len(tags) == 0 || len(tags[0]) == 0 {
tag = fieldT.Name tag = fieldT.Name
} else {
tag = tags[0]
} }
value := form.Get(tag) value := form.Get(tag)
if len(value) == 0 { if len(value) == 0 {
continue continue

View File

@ -106,8 +106,9 @@ func TestParseForm(t *testing.T) {
Id int Id int
tag string `form:tag` tag string `form:tag`
Name interface{} `form:"username"` Name interface{} `form:"username"`
Age int `form:"age"` Age int `form:"age,text"`
Email string Email string
Intro string `form:",textarea"`
} }
u := user{} u := user{}
@ -116,6 +117,7 @@ func TestParseForm(t *testing.T) {
"username": []string{"test"}, "username": []string{"test"},
"age": []string{"40"}, "age": []string{"40"},
"Email": []string{"test@gmail.com"}, "Email": []string{"test@gmail.com"},
"Intro": []string{"I am an engineer!"},
} }
if err := ParseForm(form, u); err == nil { if err := ParseForm(form, u); err == nil {
t.Fatal("nothing will be changed") t.Fatal("nothing will be changed")
@ -127,15 +129,18 @@ func TestParseForm(t *testing.T) {
t.Errorf("Id should equal 0 but got %v", u.Id) t.Errorf("Id should equal 0 but got %v", u.Id)
} }
if len(u.tag) != 0 { if len(u.tag) != 0 {
t.Error("tag's length should equal 0 but got %v", len(u.tag)) t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
} }
if u.Name.(string) != "test" { if u.Name.(string) != "test" {
t.Error("Name should equal `test` but got `%v`", u.Name.(string)) t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
} }
if u.Age != 40 { if u.Age != 40 {
t.Error("Age should equal 40 but got %v", u.Age) t.Errorf("Age should equal 40 but got %v", u.Age)
} }
if u.Email != "test@gmail.com" { if u.Email != "test@gmail.com" {
t.Error("Email should equal `test@gmail.com` but got `%v`", u.Email) t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email)
}
if u.Intro != "I am an engineer!" {
t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro)
} }
} }