renderform ignore struct field if form tag value is '-'
Showing
2 changed files
with
15 additions
and
10 deletions
| ... | @@ -282,20 +282,24 @@ func RenderForm(obj interface{}) template.HTML { | ... | @@ -282,20 +282,24 @@ func RenderForm(obj interface{}) template.HTML { |
| 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 | name := fieldT.Name | 284 | name := fieldT.Name |
| 285 | if len(tags) < 2 { | 285 | fType := "text" |
| 286 | if len(tags) > 0 && tags[0] == "-" { | ||
| 287 | continue | ||
| 288 | } | ||
| 289 | |||
| 286 | if len(tags) == 1 && len(tags[0]) > 0 { | 290 | if len(tags) == 1 && len(tags[0]) > 0 { |
| 287 | name = tags[0] | 291 | name = tags[0] |
| 288 | } | 292 | } else if len(tags) >= 2 { |
| 289 | raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="text" value="%v">`, | ||
| 290 | fieldT.Name, name, fieldV.Interface())) | ||
| 291 | } else { | ||
| 292 | if len(tags[0]) > 0 { | 293 | if len(tags[0]) > 0 { |
| 293 | name = tags[0] | 294 | name = tags[0] |
| 294 | } | 295 | } |
| 295 | raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`, | 296 | if len(tags[1]) > 0 { |
| 296 | fieldT.Name, name, tags[1], fieldV.Interface())) | 297 | fType = tags[1] |
| 297 | } | 298 | } |
| 298 | } | 299 | } |
| 300 | raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`, | ||
| 301 | fieldT.Name, name, fType, fieldV.Interface())) | ||
| 302 | } | ||
| 299 | return template.HTML(strings.Join(raw, "</br>")) | 303 | return template.HTML(strings.Join(raw, "</br>")) |
| 300 | } | 304 | } |
| 301 | 305 | ... | ... |
| ... | @@ -150,10 +150,11 @@ func TestParseForm(t *testing.T) { | ... | @@ -150,10 +150,11 @@ func TestParseForm(t *testing.T) { |
| 150 | 150 | ||
| 151 | func TestRenderForm(t *testing.T) { | 151 | func TestRenderForm(t *testing.T) { |
| 152 | type user struct { | 152 | type user struct { |
| 153 | Id int | 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 | Email []string | 158 | Email []string |
| 158 | Intro string `form:",textarea"` | 159 | Intro string `form:",textarea"` |
| 159 | } | 160 | } |
| ... | @@ -165,9 +166,9 @@ func TestRenderForm(t *testing.T) { | ... | @@ -165,9 +166,9 @@ func TestRenderForm(t *testing.T) { |
| 165 | } | 166 | } |
| 166 | output = RenderForm(&u) | 167 | output = RenderForm(&u) |
| 167 | result := template.HTML( | 168 | result := template.HTML( |
| 168 | `Id: <input name="Id" type="text" value="0"></br>` + | ||
| 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 | `Age: <input name="age" type="text" value="0"></br>` + |
| 171 | `Sex: <input name="Sex" type="text" value=""></br>` + | ||
| 171 | `Intro: <input name="Intro" type="textarea" value="">`) | 172 | `Intro: <input name="Intro" type="textarea" value="">`) |
| 172 | if output != result { | 173 | if output != result { |
| 173 | t.Errorf("output should equal `%v` but got `%v`", result, output) | 174 | t.Errorf("output should equal `%v` but got `%v`", result, output) | ... | ... |
-
Please register or sign in to post a comment