1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 12:51:29 +00:00

Merge pull request #1993 from NerdsvilleCEO/develop

Add meta fields with required flag on RenderForm
This commit is contained in:
astaxie 2016-07-14 10:47:21 +08:00 committed by GitHub
commit a2e63a3820
2 changed files with 55 additions and 19 deletions

View File

@ -421,18 +421,18 @@ func RenderForm(obj interface{}) template.HTML {
fieldT := objT.Field(i) fieldT := objT.Field(i)
label, name, fType, id, class, ignored := parseFormTag(fieldT) label, name, fType, id, class, ignored, required := parseFormTag(fieldT)
if ignored { if ignored {
continue continue
} }
raw = append(raw, renderFormField(label, name, fType, fieldV.Interface(), id, class)) raw = append(raw, renderFormField(label, name, fType, fieldV.Interface(), id, class, required))
} }
return template.HTML(strings.Join(raw, "</br>")) return template.HTML(strings.Join(raw, "</br>"))
} }
// renderFormField returns a string containing HTML of a single form field. // renderFormField returns a string containing HTML of a single form field.
func renderFormField(label, name, fType string, value interface{}, id string, class string) string { func renderFormField(label, name, fType string, value interface{}, id string, class string, required bool) string {
if id != "" { if id != "" {
id = " id=\"" + id + "\"" id = " id=\"" + id + "\""
} }
@ -441,11 +441,16 @@ func renderFormField(label, name, fType string, value interface{}, id string, cl
class = " class=\"" + class + "\"" class = " class=\"" + class + "\""
} }
if isValidForInput(fType) { requiredString := ""
return fmt.Sprintf(`%v<input%v%v name="%v" type="%v" value="%v">`, label, id, class, name, fType, value) if required {
requiredString = " required"
} }
return fmt.Sprintf(`%v<%v%v%v name="%v">%v</%v>`, label, fType, id, class, name, value, fType) if isValidForInput(fType) {
return fmt.Sprintf(`%v<input%v%v name="%v" type="%v" value="%v"%v>`, label, id, class, name, fType, value, requiredString)
}
return fmt.Sprintf(`%v<%v%v%v name="%v"%v>%v</%v>`, label, fType, id, class, name, requiredString, value, fType)
} }
// isValidForInput checks if fType is a valid value for the `type` property of an HTML input element. // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element.
@ -461,7 +466,7 @@ func isValidForInput(fType string) bool {
// parseFormTag takes the stuct-tag of a StructField and parses the `form` value. // 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. // returned are the form label, name-property, type and wether the field should be ignored.
func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id string, class string, ignored bool) { func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id string, class string, ignored bool, required bool) {
tags := strings.Split(fieldT.Tag.Get("form"), ",") tags := strings.Split(fieldT.Tag.Get("form"), ",")
label = fieldT.Name + ": " label = fieldT.Name + ": "
name = fieldT.Name name = fieldT.Name
@ -470,6 +475,12 @@ func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id str
id = fieldT.Tag.Get("id") id = fieldT.Tag.Get("id")
class = fieldT.Tag.Get("class") class = fieldT.Tag.Get("class")
required = false
required_field := fieldT.Tag.Get("required")
if required_field != "-" && required_field != "" {
required, _ = strconv.ParseBool(required_field)
}
switch len(tags) { switch len(tags) {
case 1: case 1:
if tags[0] == "-" { if tags[0] == "-" {
@ -496,6 +507,7 @@ func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id str
label = tags[2] label = tags[2]
} }
} }
return return
} }

View File

@ -195,15 +195,20 @@ func TestRenderForm(t *testing.T) {
} }
func TestRenderFormField(t *testing.T) { func TestRenderFormField(t *testing.T) {
html := renderFormField("Label: ", "Name", "text", "Value", "", "") html := renderFormField("Label: ", "Name", "text", "Value", "", "", false)
if html != `Label: <input name="Name" type="text" value="Value">` { if html != `Label: <input name="Name" type="text" value="Value">` {
t.Errorf("Wrong html output for input[type=text]: %v ", html) t.Errorf("Wrong html output for input[type=text]: %v ", html)
} }
html = renderFormField("Label: ", "Name", "textarea", "Value", "", "") html = renderFormField("Label: ", "Name", "textarea", "Value", "", "", false)
if html != `Label: <textarea name="Name">Value</textarea>` { if html != `Label: <textarea name="Name">Value</textarea>` {
t.Errorf("Wrong html output for textarea: %v ", html) t.Errorf("Wrong html output for textarea: %v ", html)
} }
html = renderFormField("Label: ", "Name", "textarea", "Value", "", "", true)
if html != `Label: <textarea name="Name" required>Value</textarea>` {
t.Errorf("Wrong html output for textarea: %v ", html)
}
} }
func TestParseFormTag(t *testing.T) { func TestParseFormTag(t *testing.T) {
@ -214,35 +219,54 @@ func TestParseFormTag(t *testing.T) {
OnlyLabel int `form:",,年龄:"` OnlyLabel int `form:",,年龄:"`
OnlyName int `form:"name" id:"name" class:"form-name"` OnlyName int `form:"name" id:"name" class:"form-name"`
Ignored int `form:"-"` Ignored int `form:"-"`
Required int `form:"name" required:"true"`
IgnoreRequired int `form:"name"`
NotRequired int `form:"name" required:"false"`
} }
objT := reflect.TypeOf(&user{}).Elem() objT := reflect.TypeOf(&user{}).Elem()
label, name, fType, id, class, ignored := parseFormTag(objT.Field(0)) label, name, fType, id, class, ignored, required := parseFormTag(objT.Field(0))
if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) { if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag with name, label and type was not correctly parsed.") t.Errorf("Form Tag with name, label and type was not correctly parsed.")
} }
label, name, fType, id, class, ignored = parseFormTag(objT.Field(1)) label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(1))
if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) { if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
t.Errorf("Form Tag with label and type but without name was not correctly parsed.") t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
} }
label, name, fType, id, class, ignored = parseFormTag(objT.Field(2)) label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(2))
if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) { if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag containing only label was not correctly parsed.") t.Errorf("Form Tag containing only label was not correctly parsed.")
} }
label, name, fType, id, class, ignored = parseFormTag(objT.Field(3)) label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(3))
if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false && if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false &&
id == "name" && class == "form-name") { id == "name" && class == "form-name") {
t.Errorf("Form Tag containing only name was not correctly parsed.") t.Errorf("Form Tag containing only name was not correctly parsed.")
} }
label, name, fType, id, class, ignored = parseFormTag(objT.Field(4)) label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(4))
if ignored == false { if ignored == false {
t.Errorf("Form Tag that should be ignored was not correctly parsed.") t.Errorf("Form Tag that should be ignored was not correctly parsed.")
} }
label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(5))
if !(name == "name" && required == true) {
t.Errorf("Form Tag containing only name and required was not correctly parsed.")
}
label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(6))
if !(name == "name" && required == false) {
t.Errorf("Form Tag containing only name and ignore required was not correctly parsed.")
}
label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(7))
if !(name == "name" && required == false) {
t.Errorf("Form Tag containing only name and not required was not correctly parsed.")
}
} }
func TestMapGet(t *testing.T) { func TestMapGet(t *testing.T) {