Support custom label for renderform
Showing
2 changed files
with
25 additions
and
10 deletions
| ... | @@ -281,24 +281,39 @@ func RenderForm(obj interface{}) template.HTML { | ... | @@ -281,24 +281,39 @@ func RenderForm(obj interface{}) template.HTML { |
| 281 | 281 | ||
| 282 | fieldT := objT.Field(i) | 282 | fieldT := objT.Field(i) |
| 283 | tags := strings.Split(fieldT.Tag.Get("form"), ",") | 283 | tags := strings.Split(fieldT.Tag.Get("form"), ",") |
| 284 | label := fieldT.Name + ": " | ||
| 284 | name := fieldT.Name | 285 | name := fieldT.Name |
| 285 | fType := "text" | 286 | fType := "text" |
| 286 | if len(tags) > 0 && tags[0] == "-" { | ||
| 287 | continue | ||
| 288 | } | ||
| 289 | 287 | ||
| 290 | if len(tags) == 1 && len(tags[0]) > 0 { | 288 | switch len(tags) { |
| 291 | name = tags[0] | 289 | case 1: |
| 292 | } else if len(tags) >= 2 { | 290 | if tags[0] == "-" { |
| 291 | continue | ||
| 292 | } | ||
| 293 | if len(tags[0]) > 0 { | ||
| 294 | name = tags[0] | ||
| 295 | } | ||
| 296 | case 2: | ||
| 297 | if len(tags[0]) > 0 { | ||
| 298 | name = tags[0] | ||
| 299 | } | ||
| 300 | if len(tags[1]) > 0 { | ||
| 301 | fType = tags[1] | ||
| 302 | } | ||
| 303 | case 3: | ||
| 293 | if len(tags[0]) > 0 { | 304 | if len(tags[0]) > 0 { |
| 294 | name = tags[0] | 305 | name = tags[0] |
| 295 | } | 306 | } |
| 296 | if len(tags[1]) > 0 { | 307 | if len(tags[1]) > 0 { |
| 297 | fType = tags[1] | 308 | fType = tags[1] |
| 298 | } | 309 | } |
| 310 | if len(tags[2]) > 0 { | ||
| 311 | label = tags[2] | ||
| 312 | } | ||
| 299 | } | 313 | } |
| 300 | raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`, | 314 | |
| 301 | fieldT.Name, name, fType, fieldV.Interface())) | 315 | raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, |
| 316 | label, name, fType, fieldV.Interface())) | ||
| 302 | } | 317 | } |
| 303 | return template.HTML(strings.Join(raw, "</br>")) | 318 | return template.HTML(strings.Join(raw, "</br>")) |
| 304 | } | 319 | } | ... | ... |
| ... | @@ -153,7 +153,7 @@ func TestRenderForm(t *testing.T) { | ... | @@ -153,7 +153,7 @@ func TestRenderForm(t *testing.T) { |
| 153 | Id int `form:"-"` | 153 | Id int `form:"-"` |
| 154 | tag string `form:"tag"` | 154 | tag string `form:"tag"` |
| 155 | Name interface{} `form:"username"` | 155 | Name interface{} `form:"username"` |
| 156 | Age int `form:"age,text"` | 156 | Age int `form:"age,text,年龄:"` |
| 157 | Sex string | 157 | Sex string |
| 158 | Email []string | 158 | Email []string |
| 159 | Intro string `form:",textarea"` | 159 | Intro string `form:",textarea"` |
| ... | @@ -167,7 +167,7 @@ func TestRenderForm(t *testing.T) { | ... | @@ -167,7 +167,7 @@ func TestRenderForm(t *testing.T) { |
| 167 | output = RenderForm(&u) | 167 | output = RenderForm(&u) |
| 168 | result := template.HTML( | 168 | result := template.HTML( |
| 169 | `Name: <input name="username" type="text" value="test"></br>` + | 169 | `Name: <input name="username" type="text" value="test"></br>` + |
| 170 | `Age: <input name="age" type="text" value="0"></br>` + | 170 | `年龄:<input name="age" type="text" value="0"></br>` + |
| 171 | `Sex: <input name="Sex" type="text" value=""></br>` + | 171 | `Sex: <input name="Sex" type="text" value=""></br>` + |
| 172 | `Intro: <input name="Intro" type="textarea" value="">`) | 172 | `Intro: <input name="Intro" type="textarea" value="">`) |
| 173 | if output != result { | 173 | if output != result { | ... | ... |
-
Please register or sign in to post a comment