1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 05:01:28 +00:00

Merge meta tag into parseFormField

This commit is contained in:
Joshua Santos 2016-06-28 15:19:58 -07:00
parent 0d3a806c23
commit f9f92b4f61

View File

@ -421,8 +421,7 @@ 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)
required := parseMetaTag(fieldT)
if ignored { if ignored {
continue continue
} }
@ -467,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
@ -476,6 +475,15 @@ 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")
meta := strings.Split(fieldT.Tag.Get("meta"), ",")
required = false
switch len(meta) {
case 1:
if len(meta[0]) > 0 && meta[0] != "-" {
required = true
}
}
switch len(tags) { switch len(tags) {
case 1: case 1:
if tags[0] == "-" { if tags[0] == "-" {
@ -506,20 +514,6 @@ func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id str
return return
} }
// parseMetaTag takes the stuct-tag of a StructField and parses the `meta` value.
// returned is the boolean of whether the field is required
func parseMetaTag(fieldT reflect.StructField) (required bool) {
meta := strings.Split(fieldT.Tag.Get("meta"), ",")
required = false
switch len(meta) {
case 1:
if len(meta[0]) > 0 && meta[0] != "-" {
required = true
}
}
return
}
func isStructPtr(t reflect.Type) bool { func isStructPtr(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
} }