diff --git a/utils.go b/utils.go index 0ab108ff..5eaee164 100644 --- a/utils.go +++ b/utils.go @@ -281,24 +281,39 @@ func RenderForm(obj interface{}) template.HTML { fieldT := objT.Field(i) tags := strings.Split(fieldT.Tag.Get("form"), ",") + label := fieldT.Name + ": " name := fieldT.Name fType := "text" - if len(tags) > 0 && tags[0] == "-" { - continue - } - if len(tags) == 1 && len(tags[0]) > 0 { - name = tags[0] - } else if len(tags) >= 2 { + switch len(tags) { + case 1: + if tags[0] == "-" { + continue + } + if len(tags[0]) > 0 { + name = tags[0] + } + case 2: if len(tags[0]) > 0 { name = tags[0] } if len(tags[1]) > 0 { fType = tags[1] } + case 3: + if len(tags[0]) > 0 { + name = tags[0] + } + if len(tags[1]) > 0 { + fType = tags[1] + } + if len(tags[2]) > 0 { + label = tags[2] + } } - raw = append(raw, fmt.Sprintf(`%v: `, - fieldT.Name, name, fType, fieldV.Interface())) + + raw = append(raw, fmt.Sprintf(`%v`, + label, name, fType, fieldV.Interface())) } return template.HTML(strings.Join(raw, "
")) } diff --git a/utils_test.go b/utils_test.go index 22416ffe..b6a5bc58 100644 --- a/utils_test.go +++ b/utils_test.go @@ -153,7 +153,7 @@ func TestRenderForm(t *testing.T) { Id int `form:"-"` tag string `form:"tag"` Name interface{} `form:"username"` - Age int `form:"age,text"` + Age int `form:"age,text,年龄:"` Sex string Email []string Intro string `form:",textarea"` @@ -167,7 +167,7 @@ func TestRenderForm(t *testing.T) { output = RenderForm(&u) result := template.HTML( `Name:
` + - `Age:
` + + `年龄:
` + `Sex:
` + `Intro: `) if output != result {