Merge pull request #873 from WithGJR/develop
add new feature to 'renderform' function, user could add HTML id and class now
Showing
1 changed file
with
16 additions
and
6 deletions
| ... | @@ -368,23 +368,31 @@ func RenderForm(obj interface{}) template.HTML { | ... | @@ -368,23 +368,31 @@ func RenderForm(obj interface{}) template.HTML { |
| 368 | 368 | ||
| 369 | fieldT := objT.Field(i) | 369 | fieldT := objT.Field(i) |
| 370 | 370 | ||
| 371 | label, name, fType, ignored := parseFormTag(fieldT) | 371 | label, name, fType, id, class, ignored := parseFormTag(fieldT) |
| 372 | if ignored { | 372 | if ignored { |
| 373 | continue | 373 | continue |
| 374 | } | 374 | } |
| 375 | 375 | ||
| 376 | raw = append(raw, renderFormField(label, name, fType, fieldV.Interface())) | 376 | raw = append(raw, renderFormField(label, name, fType, fieldV.Interface(), id, class)) |
| 377 | } | 377 | } |
| 378 | return template.HTML(strings.Join(raw, "</br>")) | 378 | return template.HTML(strings.Join(raw, "</br>")) |
| 379 | } | 379 | } |
| 380 | 380 | ||
| 381 | // renderFormField returns a string containing HTML of a single form field. | 381 | // renderFormField returns a string containing HTML of a single form field. |
| 382 | func renderFormField(label, name, fType string, value interface{}) string { | 382 | func renderFormField(label, name, fType string, value interface{}, id string, class string) string { |
| 383 | if id != "" { | ||
| 384 | id = "id=\"" + id + "\"" | ||
| 385 | } | ||
| 386 | |||
| 387 | if class != "" { | ||
| 388 | class = "class=\"" + class + "\"" | ||
| 389 | } | ||
| 390 | |||
| 383 | if isValidForInput(fType) { | 391 | if isValidForInput(fType) { |
| 384 | return fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, label, name, fType, value) | 392 | return fmt.Sprintf(`%v<input %v %v name="%v" type="%v" value="%v">`, label, id, class, name, fType, value) |
| 385 | } | 393 | } |
| 386 | 394 | ||
| 387 | return fmt.Sprintf(`%v<%v name="%v">%v</%v>`, label, fType, name, value, fType) | 395 | return fmt.Sprintf(`%v<%v %v %v name="%v">%v</%v>`, label, fType, id, class, name, value, fType) |
| 388 | } | 396 | } |
| 389 | 397 | ||
| 390 | // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element. | 398 | // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element. |
| ... | @@ -400,12 +408,14 @@ func isValidForInput(fType string) bool { | ... | @@ -400,12 +408,14 @@ func isValidForInput(fType string) bool { |
| 400 | 408 | ||
| 401 | // parseFormTag takes the stuct-tag of a StructField and parses the `form` value. | 409 | // parseFormTag takes the stuct-tag of a StructField and parses the `form` value. |
| 402 | // returned are the form label, name-property, type and wether the field should be ignored. | 410 | // returned are the form label, name-property, type and wether the field should be ignored. |
| 403 | func parseFormTag(fieldT reflect.StructField) (label, name, fType string, ignored bool) { | 411 | func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id string, class string, ignored bool) { |
| 404 | tags := strings.Split(fieldT.Tag.Get("form"), ",") | 412 | tags := strings.Split(fieldT.Tag.Get("form"), ",") |
| 405 | label = fieldT.Name + ": " | 413 | label = fieldT.Name + ": " |
| 406 | name = fieldT.Name | 414 | name = fieldT.Name |
| 407 | fType = "text" | 415 | fType = "text" |
| 408 | ignored = false | 416 | ignored = false |
| 417 | id = fieldT.Tag.Get("id") | ||
| 418 | class = fieldT.Tag.Get("class") | ||
| 409 | 419 | ||
| 410 | switch len(tags) { | 420 | switch len(tags) { |
| 411 | case 1: | 421 | case 1: | ... | ... |
-
Please register or sign in to post a comment