Merge branch 'develop' of https://github.com/astaxie/beego into develop
Showing
2 changed files
with
32 additions
and
0 deletions
| ... | @@ -302,6 +302,14 @@ func ParseForm(form url.Values, obj interface{}) error { | ... | @@ -302,6 +302,14 @@ func ParseForm(form url.Values, obj interface{}) error { |
| 302 | 302 | ||
| 303 | switch fieldT.Type.Kind() { | 303 | switch fieldT.Type.Kind() { |
| 304 | case reflect.Bool: | 304 | case reflect.Bool: |
| 305 | if strings.ToLower(value) == "on" || strings.ToLower(value) == "1" || strings.ToLower(value) == "yes" { | ||
| 306 | fieldV.SetBool(true) | ||
| 307 | continue | ||
| 308 | } | ||
| 309 | if strings.ToLower(value) == "off" || strings.ToLower(value) == "0" || strings.ToLower(value) == "no" { | ||
| 310 | fieldV.SetBool(false) | ||
| 311 | continue | ||
| 312 | } | ||
| 305 | b, err := strconv.ParseBool(value) | 313 | b, err := strconv.ParseBool(value) |
| 306 | if err != nil { | 314 | if err != nil { |
| 307 | return err | 315 | return err |
| ... | @@ -329,6 +337,19 @@ func ParseForm(form url.Values, obj interface{}) error { | ... | @@ -329,6 +337,19 @@ func ParseForm(form url.Values, obj interface{}) error { |
| 329 | fieldV.Set(reflect.ValueOf(value)) | 337 | fieldV.Set(reflect.ValueOf(value)) |
| 330 | case reflect.String: | 338 | case reflect.String: |
| 331 | fieldV.SetString(value) | 339 | fieldV.SetString(value) |
| 340 | case reflect.Struct: | ||
| 341 | switch fieldT.Type.String() { | ||
| 342 | case "time.Time": | ||
| 343 | format := time.RFC3339 | ||
| 344 | if len(tags) > 1 { | ||
| 345 | format = tags[1] | ||
| 346 | } | ||
| 347 | t, err := time.Parse(format, value) | ||
| 348 | if err != nil { | ||
| 349 | return err | ||
| 350 | } | ||
| 351 | fieldV.Set(reflect.ValueOf(t)) | ||
| 352 | } | ||
| 332 | } | 353 | } |
| 333 | } | 354 | } |
| 334 | return nil | 355 | return nil | ... | ... |
| ... | @@ -108,6 +108,8 @@ func TestParseForm(t *testing.T) { | ... | @@ -108,6 +108,8 @@ func TestParseForm(t *testing.T) { |
| 108 | Age int `form:"age,text"` | 108 | Age int `form:"age,text"` |
| 109 | Email string | 109 | Email string |
| 110 | Intro string `form:",textarea"` | 110 | Intro string `form:",textarea"` |
| 111 | StrBool bool `form:"strbool"` | ||
| 112 | Date time.Time `form:"date,2006-01-02"` | ||
| 111 | } | 113 | } |
| 112 | 114 | ||
| 113 | u := user{} | 115 | u := user{} |
| ... | @@ -119,6 +121,8 @@ func TestParseForm(t *testing.T) { | ... | @@ -119,6 +121,8 @@ func TestParseForm(t *testing.T) { |
| 119 | "age": []string{"40"}, | 121 | "age": []string{"40"}, |
| 120 | "Email": []string{"test@gmail.com"}, | 122 | "Email": []string{"test@gmail.com"}, |
| 121 | "Intro": []string{"I am an engineer!"}, | 123 | "Intro": []string{"I am an engineer!"}, |
| 124 | "strbool": []string{"yes"}, | ||
| 125 | "date": []string{"2014-11-12"}, | ||
| 122 | } | 126 | } |
| 123 | if err := ParseForm(form, u); err == nil { | 127 | if err := ParseForm(form, u); err == nil { |
| 124 | t.Fatal("nothing will be changed") | 128 | t.Fatal("nothing will be changed") |
| ... | @@ -144,6 +148,13 @@ func TestParseForm(t *testing.T) { | ... | @@ -144,6 +148,13 @@ func TestParseForm(t *testing.T) { |
| 144 | if u.Intro != "I am an engineer!" { | 148 | if u.Intro != "I am an engineer!" { |
| 145 | t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro) | 149 | t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro) |
| 146 | } | 150 | } |
| 151 | if u.StrBool != true { | ||
| 152 | t.Errorf("strboll should equal `true`, but got `%v`", u.StrBool) | ||
| 153 | } | ||
| 154 | y, m, d := u.Date.Date() | ||
| 155 | if y != 2014 || m.String() != "November" || d != 12 { | ||
| 156 | t.Errorf("Date should equal `2014-11-12`, but got `%v`", u.Date.String()) | ||
| 157 | } | ||
| 147 | } | 158 | } |
| 148 | 159 | ||
| 149 | func TestRenderForm(t *testing.T) { | 160 | func TestRenderForm(t *testing.T) { | ... | ... |
-
Please register or sign in to post a comment