templateform.RenderForm now renders textarea
When RenderForm encounters a field with the structTag `form` value type `textarea` it renders an actual <textarea> html tag.
Showing
2 changed files
with
35 additions
and
5 deletions
| ... | @@ -327,7 +327,6 @@ func ParseForm(form url.Values, obj interface{}) error { | ... | @@ -327,7 +327,6 @@ func ParseForm(form url.Values, obj interface{}) error { |
| 327 | return nil | 327 | return nil |
| 328 | } | 328 | } |
| 329 | 329 | ||
| 330 | |||
| 331 | var unKind = map[reflect.Kind]bool{ | 330 | var unKind = map[reflect.Kind]bool{ |
| 332 | reflect.Uintptr: true, | 331 | reflect.Uintptr: true, |
| 333 | reflect.Complex64: true, | 332 | reflect.Complex64: true, |
| ... | @@ -367,12 +366,30 @@ func RenderForm(obj interface{}) template.HTML { | ... | @@ -367,12 +366,30 @@ func RenderForm(obj interface{}) template.HTML { |
| 367 | continue | 366 | continue |
| 368 | } | 367 | } |
| 369 | 368 | ||
| 370 | raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, | 369 | raw = append(raw, renderFormField(label, name, fType, fieldV.Interface())) |
| 371 | label, name, fType, fieldV.Interface())) | ||
| 372 | } | 370 | } |
| 373 | return template.HTML(strings.Join(raw, "</br>")) | 371 | return template.HTML(strings.Join(raw, "</br>")) |
| 374 | } | 372 | } |
| 375 | 373 | ||
| 374 | func renderFormField(label, name, fType string, value interface{}) string { | ||
| 375 | if isValidForInput(fType) { | ||
| 376 | return fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, label, name, fType, value) | ||
| 377 | } | ||
| 378 | |||
| 379 | return fmt.Sprintf(`%v<%v name="%v">%v</%v>`, label, fType, name, value, fType) | ||
| 380 | } | ||
| 381 | |||
| 382 | func isValidForInput(fType string) bool { | ||
| 383 | validInputTypes := strings.Fields("text password checkbox radio submit reset hidden image file button") | ||
| 384 | for _, validType := range validInputTypes { | ||
| 385 | if fType == validType { | ||
| 386 | return true | ||
| 387 | } | ||
| 388 | } | ||
| 389 | return false | ||
| 390 | } | ||
| 391 | |||
| 392 | |||
| 376 | // parseFormTag takes the stuct-tag of a StructField and parses the `form` value. | 393 | // parseFormTag takes the stuct-tag of a StructField and parses the `form` value. |
| 377 | // returned are the form label, name-property, type and wether the field should be ignored. | 394 | // returned are the form label, name-property, type and wether the field should be ignored. |
| 378 | func parseFormTag(fieldT reflect.StructField) (label, name, fType string, ignored bool) { | 395 | func parseFormTag(fieldT reflect.StructField) (label, name, fType string, ignored bool) { | ... | ... |
| ... | @@ -148,9 +148,10 @@ func TestRenderForm(t *testing.T) { | ... | @@ -148,9 +148,10 @@ func TestRenderForm(t *testing.T) { |
| 148 | Sex string | 148 | Sex string |
| 149 | Email []string | 149 | Email []string |
| 150 | Intro string `form:",textarea"` | 150 | Intro string `form:",textarea"` |
| 151 | Ignored string `form:"-"` | ||
| 151 | } | 152 | } |
| 152 | 153 | ||
| 153 | u := user{Name: "test"} | 154 | u := user{Name: "test", Intro: "Some Text"} |
| 154 | output := RenderForm(u) | 155 | output := RenderForm(u) |
| 155 | if output != template.HTML("") { | 156 | if output != template.HTML("") { |
| 156 | t.Errorf("output should be empty but got %v", output) | 157 | t.Errorf("output should be empty but got %v", output) |
| ... | @@ -160,12 +161,24 @@ func TestRenderForm(t *testing.T) { | ... | @@ -160,12 +161,24 @@ func TestRenderForm(t *testing.T) { |
| 160 | `Name: <input name="username" type="text" value="test"></br>` + | 161 | `Name: <input name="username" type="text" value="test"></br>` + |
| 161 | `年龄:<input name="age" type="text" value="0"></br>` + | 162 | `年龄:<input name="age" type="text" value="0"></br>` + |
| 162 | `Sex: <input name="Sex" type="text" value=""></br>` + | 163 | `Sex: <input name="Sex" type="text" value=""></br>` + |
| 163 | `Intro: <input name="Intro" type="textarea" value="">`) | 164 | `Intro: <textarea name="Intro">Some Text</textarea>`) |
| 164 | if output != result { | 165 | if output != result { |
| 165 | t.Errorf("output should equal `%v` but got `%v`", result, output) | 166 | t.Errorf("output should equal `%v` but got `%v`", result, output) |
| 166 | } | 167 | } |
| 167 | } | 168 | } |
| 168 | 169 | ||
| 170 | func TestRenderFormField(t *testing.T) { | ||
| 171 | html := renderFormField("Label: ", "Name", "text", "Value") | ||
| 172 | if html != `Label: <input name="Name" type="text" value="Value">` { | ||
| 173 | t.Errorf("Wrong html output for input[type=text]: %v ", html) | ||
| 174 | } | ||
| 175 | |||
| 176 | html = renderFormField("Label: ", "Name", "textarea", "Value") | ||
| 177 | if html != `Label: <textarea name="Name">Value</textarea>` { | ||
| 178 | t.Errorf("Wrong html output for textarea: %v ", html) | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 169 | func TestParseFormTag(t *testing.T) { | 182 | func TestParseFormTag(t *testing.T) { |
| 170 | // create struct to contain field with different types of struct-tag `form` | 183 | // create struct to contain field with different types of struct-tag `form` |
| 171 | type user struct { | 184 | type user struct { | ... | ... |
-
Please register or sign in to post a comment