ca1354e7 by astaxie

Merge pull request #147 from miraclesu/form

ignore struct field if form tag value is '-'
2 parents 18c09bb2 459b9785
...@@ -195,6 +195,8 @@ func ParseForm(form url.Values, obj interface{}) error { ...@@ -195,6 +195,8 @@ func ParseForm(form url.Values, obj interface{}) error {
195 var tag string 195 var tag string
196 if len(tags) == 0 || len(tags[0]) == 0 { 196 if len(tags) == 0 || len(tags[0]) == 0 {
197 tag = fieldT.Name 197 tag = fieldT.Name
198 } else if tags[0] == "-" {
199 continue
198 } else { 200 } else {
199 tag = tags[0] 201 tag = tags[0]
200 } 202 }
...@@ -280,20 +282,24 @@ func RenderForm(obj interface{}) template.HTML { ...@@ -280,20 +282,24 @@ func RenderForm(obj interface{}) template.HTML {
280 fieldT := objT.Field(i) 282 fieldT := objT.Field(i)
281 tags := strings.Split(fieldT.Tag.Get("form"), ",") 283 tags := strings.Split(fieldT.Tag.Get("form"), ",")
282 name := fieldT.Name 284 name := fieldT.Name
283 if len(tags) < 2 { 285 fType := "text"
286 if len(tags) > 0 && tags[0] == "-" {
287 continue
288 }
289
284 if len(tags) == 1 && len(tags[0]) > 0 { 290 if len(tags) == 1 && len(tags[0]) > 0 {
285 name = tags[0] 291 name = tags[0]
286 } 292 } else if len(tags) >= 2 {
287 raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="text" value="%v">`,
288 fieldT.Name, name, fieldV.Interface()))
289 } else {
290 if len(tags[0]) > 0 { 293 if len(tags[0]) > 0 {
291 name = tags[0] 294 name = tags[0]
292 } 295 }
293 raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`, 296 if len(tags[1]) > 0 {
294 fieldT.Name, name, tags[1], fieldV.Interface())) 297 fType = tags[1]
295 } 298 }
296 } 299 }
300 raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`,
301 fieldT.Name, name, fType, fieldV.Interface()))
302 }
297 return template.HTML(strings.Join(raw, "</br>")) 303 return template.HTML(strings.Join(raw, "</br>"))
298 } 304 }
299 305
......
...@@ -104,8 +104,8 @@ func TestInSlice(t *testing.T) { ...@@ -104,8 +104,8 @@ func TestInSlice(t *testing.T) {
104 104
105 func TestParseForm(t *testing.T) { 105 func TestParseForm(t *testing.T) {
106 type user struct { 106 type user struct {
107 Id int 107 Id int `form:"-"`
108 tag string `form:tag` 108 tag string `form:"tag"`
109 Name interface{} `form:"username"` 109 Name interface{} `form:"username"`
110 Age int `form:"age,text"` 110 Age int `form:"age,text"`
111 Email string 111 Email string
...@@ -114,6 +114,8 @@ func TestParseForm(t *testing.T) { ...@@ -114,6 +114,8 @@ func TestParseForm(t *testing.T) {
114 114
115 u := user{} 115 u := user{}
116 form := url.Values{ 116 form := url.Values{
117 "Id": []string{"1"},
118 "-": []string{"1"},
117 "tag": []string{"no"}, 119 "tag": []string{"no"},
118 "username": []string{"test"}, 120 "username": []string{"test"},
119 "age": []string{"40"}, 121 "age": []string{"40"},
...@@ -148,10 +150,11 @@ func TestParseForm(t *testing.T) { ...@@ -148,10 +150,11 @@ func TestParseForm(t *testing.T) {
148 150
149 func TestRenderForm(t *testing.T) { 151 func TestRenderForm(t *testing.T) {
150 type user struct { 152 type user struct {
151 Id int 153 Id int `form:"-"`
152 tag string `form:tag` 154 tag string `form:"tag"`
153 Name interface{} `form:"username"` 155 Name interface{} `form:"username"`
154 Age int `form:"age,text"` 156 Age int `form:"age,text"`
157 Sex string
155 Email []string 158 Email []string
156 Intro string `form:",textarea"` 159 Intro string `form:",textarea"`
157 } 160 }
...@@ -163,9 +166,9 @@ func TestRenderForm(t *testing.T) { ...@@ -163,9 +166,9 @@ func TestRenderForm(t *testing.T) {
163 } 166 }
164 output = RenderForm(&u) 167 output = RenderForm(&u)
165 result := template.HTML( 168 result := template.HTML(
166 `Id: <input name="Id" type="text" value="0"></br>` +
167 `Name: <input name="username" type="text" value="test"></br>` + 169 `Name: <input name="username" type="text" value="test"></br>` +
168 `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>` +
169 `Intro: <input name="Intro" type="textarea" value="">`) 172 `Intro: <input name="Intro" type="textarea" value="">`)
170 if output != result { 173 if output != result {
171 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!