From 1b3e7de4639384b3d1f33e13885f8b32eda1b719 Mon Sep 17 00:00:00 2001 From: WithGJR Date: Mon, 20 Oct 2014 17:49:16 +0800 Subject: [PATCH 1/2] add new feature to 'renderform' function, user could add HTML id and class now --- templatefunc.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/templatefunc.go b/templatefunc.go index a365718d..455f3d3f 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -368,23 +368,23 @@ func RenderForm(obj interface{}) template.HTML { fieldT := objT.Field(i) - label, name, fType, ignored := parseFormTag(fieldT) + label, name, fType, id, class, ignored := parseFormTag(fieldT) if ignored { continue } - raw = append(raw, renderFormField(label, name, fType, fieldV.Interface())) + raw = append(raw, renderFormField(label, name, fType, fieldV.Interface(), id, class)) } return template.HTML(strings.Join(raw, "
")) } // renderFormField returns a string containing HTML of a single form field. -func renderFormField(label, name, fType string, value interface{}) string { +func renderFormField(label, name, fType string, value interface{}, id string, class string) string { if isValidForInput(fType) { - return fmt.Sprintf(`%v`, label, name, fType, value) + return fmt.Sprintf(`%v`, label, id, class, name, fType, value) } - return fmt.Sprintf(`%v<%v name="%v">%v`, label, fType, name, value, fType) + return fmt.Sprintf(`%v<%v id="%v" class="%v" name="%v">%v`, label, fType, id, class, name, value, fType) } // 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 { // parseFormTag takes the stuct-tag of a StructField and parses the `form` value. // returned are the form label, name-property, type and wether the field should be ignored. -func parseFormTag(fieldT reflect.StructField) (label, name, fType string, ignored bool) { +func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id string, class string, ignored bool) { tags := strings.Split(fieldT.Tag.Get("form"), ",") label = fieldT.Name + ": " name = fieldT.Name fType = "text" ignored = false + id = fieldT.Tag.Get("id") + class = fieldT.Tag.Get("class") switch len(tags) { case 1: From 6c9ff81fc160888873396f107126dd917dabc037 Mon Sep 17 00:00:00 2001 From: WithGJR Date: Mon, 20 Oct 2014 18:59:46 +0800 Subject: [PATCH 2/2] fix: if user didn't set id or class, then it won't be displayed in HTML code --- templatefunc.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/templatefunc.go b/templatefunc.go index 455f3d3f..4c87762f 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -380,11 +380,19 @@ func RenderForm(obj interface{}) template.HTML { // renderFormField returns a string containing HTML of a single form field. func renderFormField(label, name, fType string, value interface{}, id string, class string) string { - if isValidForInput(fType) { - return fmt.Sprintf(`%v`, label, id, class, name, fType, value) + if id != "" { + id = "id=\"" + id + "\"" } - return fmt.Sprintf(`%v<%v id="%v" class="%v" name="%v">%v`, label, fType, id, class, name, value, fType) + if class != "" { + class = "class=\"" + class + "\"" + } + + if isValidForInput(fType) { + return fmt.Sprintf(`%v`, label, id, class, name, fType, value) + } + + return fmt.Sprintf(`%v<%v %v %v name="%v">%v`, label, fType, id, class, name, value, fType) } // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element.