459b9785 by miraclesu

renderform ignore struct field if form tag value is '-'

1 parent 5c859466
...@@ -282,19 +282,23 @@ func RenderForm(obj interface{}) template.HTML { ...@@ -282,19 +282,23 @@ 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) == 1 && len(tags[0]) > 0 { 286 if len(tags) > 0 && tags[0] == "-" {
287 name = tags[0] 287 continue
288 } 288 }
289 raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="text" value="%v">`, 289
290 fieldT.Name, name, fieldV.Interface())) 290 if len(tags) == 1 && len(tags[0]) > 0 {
291 } else { 291 name = tags[0]
292 } else if len(tags) >= 2 {
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]
298 }
297 } 299 }
300 raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`,
301 fieldT.Name, name, fType, fieldV.Interface()))
298 } 302 }
299 return template.HTML(strings.Join(raw, "</br>")) 303 return template.HTML(strings.Join(raw, "</br>"))
300 } 304 }
......
...@@ -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)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!