diff --git a/templatefunc.go b/templatefunc.go
index 4c87762f..99f0a3e8 100644
--- a/templatefunc.go
+++ b/templatefunc.go
@@ -381,18 +381,18 @@ 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 id != "" {
- id = "id=\"" + id + "\""
+ id = " id=\"" + id + "\""
}
if class != "" {
- class = "class=\"" + class + "\""
+ class = " class=\"" + class + "\""
}
if isValidForInput(fType) {
- return fmt.Sprintf(`%v`, label, id, class, name, fType, value)
+ return fmt.Sprintf(`%v`, label, id, class, name, fType, value)
}
- return fmt.Sprintf(`%v<%v %v %v name="%v">%v%v>`, label, fType, id, class, name, value, fType)
+ return fmt.Sprintf(`%v<%v%v%v name="%v">%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.
diff --git a/templatefunc_test.go b/templatefunc_test.go
index 44a06dec..9a461c9f 100644
--- a/templatefunc_test.go
+++ b/templatefunc_test.go
@@ -175,12 +175,12 @@ func TestRenderForm(t *testing.T) {
}
func TestRenderFormField(t *testing.T) {
- html := renderFormField("Label: ", "Name", "text", "Value")
+ html := renderFormField("Label: ", "Name", "text", "Value", "", "")
if html != `Label: ` {
t.Errorf("Wrong html output for input[type=text]: %v ", html)
}
- html = renderFormField("Label: ", "Name", "textarea", "Value")
+ html = renderFormField("Label: ", "Name", "textarea", "Value", "", "")
if html != `Label: ` {
t.Errorf("Wrong html output for textarea: %v ", html)
}
@@ -192,33 +192,34 @@ func TestParseFormTag(t *testing.T) {
All int `form:"name,text,年龄:"`
NoName int `form:",hidden,年龄:"`
OnlyLabel int `form:",,年龄:"`
- OnlyName int `form:"name"`
+ OnlyName int `form:"name" id:"name" class:"form-name"`
Ignored int `form:"-"`
}
objT := reflect.TypeOf(&user{}).Elem()
- label, name, fType, ignored := parseFormTag(objT.Field(0))
+ label, name, fType, id, class, ignored := parseFormTag(objT.Field(0))
if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag with name, label and type was not correctly parsed.")
}
- label, name, fType, ignored = parseFormTag(objT.Field(1))
+ label, name, fType, id, class, ignored = parseFormTag(objT.Field(1))
if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
}
- label, name, fType, ignored = parseFormTag(objT.Field(2))
+ label, name, fType, id, class, ignored = parseFormTag(objT.Field(2))
if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag containing only label was not correctly parsed.")
}
- label, name, fType, ignored = parseFormTag(objT.Field(3))
- if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false) {
+ label, name, fType, id, class, ignored = parseFormTag(objT.Field(3))
+ if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false &&
+ id == "name" && class == "form-name") {
t.Errorf("Form Tag containing only name was not correctly parsed.")
}
- label, name, fType, ignored = parseFormTag(objT.Field(4))
+ label, name, fType, id, class, ignored = parseFormTag(objT.Field(4))
if ignored == false {
t.Errorf("Form Tag that should be ignored was not correctly parsed.")
}