mirror of
https://github.com/astaxie/beego.git
synced 2024-11-16 21:30:56 +00:00
Merge pull request #873 from WithGJR/develop
add new feature to 'renderform' function, user could add HTML id and class now
This commit is contained in:
commit
dbf944adce
@ -368,23 +368,31 @@ func RenderForm(obj interface{}) template.HTML {
|
|||||||
|
|
||||||
fieldT := objT.Field(i)
|
fieldT := objT.Field(i)
|
||||||
|
|
||||||
label, name, fType, ignored := parseFormTag(fieldT)
|
label, name, fType, id, class, ignored := parseFormTag(fieldT)
|
||||||
if ignored {
|
if ignored {
|
||||||
continue
|
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, "</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{}) string {
|
func renderFormField(label, name, fType string, value interface{}, id string, class string) string {
|
||||||
if isValidForInput(fType) {
|
if id != "" {
|
||||||
return fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, label, name, fType, value)
|
id = "id=\"" + id + "\""
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(`%v<%v name="%v">%v</%v>`, label, fType, name, value, fType)
|
if class != "" {
|
||||||
|
class = "class=\"" + class + "\""
|
||||||
|
}
|
||||||
|
|
||||||
|
if isValidForInput(fType) {
|
||||||
|
return fmt.Sprintf(`%v<input %v %v name="%v" type="%v" value="%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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@ -400,12 +408,14 @@ 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, ignored bool) {
|
func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id string, class string, ignored 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
|
||||||
fType = "text"
|
fType = "text"
|
||||||
ignored = false
|
ignored = false
|
||||||
|
id = fieldT.Tag.Get("id")
|
||||||
|
class = fieldT.Tag.Get("class")
|
||||||
|
|
||||||
switch len(tags) {
|
switch len(tags) {
|
||||||
case 1:
|
case 1:
|
||||||
|
Loading…
Reference in New Issue
Block a user