1b3e7de4 by WithGJR

add new feature to 'renderform' function, user could add HTML id and class now

1 parent efc14a1e
...@@ -368,23 +368,23 @@ func RenderForm(obj interface{}) template.HTML { ...@@ -368,23 +368,23 @@ 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 isValidForInput(fType) { 383 if isValidForInput(fType) {
384 return fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, label, name, fType, value) 384 return fmt.Sprintf(`%v<input id="%v" class="%v" name="%v" type="%v" value="%v">`, label, id, class, name, fType, value)
385 } 385 }
386 386
387 return fmt.Sprintf(`%v<%v name="%v">%v</%v>`, label, fType, name, value, fType) 387 return fmt.Sprintf(`%v<%v id="%v" class="%v" name="%v">%v</%v>`, label, fType, id, class, name, value, fType)
388 } 388 }
389 389
390 // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element. 390 // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element.
...@@ -400,12 +400,14 @@ func isValidForInput(fType string) bool { ...@@ -400,12 +400,14 @@ func isValidForInput(fType string) bool {
400 400
401 // parseFormTag takes the stuct-tag of a StructField and parses the `form` value. 401 // 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. 402 // 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) { 403 func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id string, class string, ignored bool) {
404 tags := strings.Split(fieldT.Tag.Get("form"), ",") 404 tags := strings.Split(fieldT.Tag.Get("form"), ",")
405 label = fieldT.Name + ": " 405 label = fieldT.Name + ": "
406 name = fieldT.Name 406 name = fieldT.Name
407 fType = "text" 407 fType = "text"
408 ignored = false 408 ignored = false
409 id = fieldT.Tag.Get("id")
410 class = fieldT.Tag.Get("class")
409 411
410 switch len(tags) { 412 switch len(tags) {
411 case 1: 413 case 1:
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!