How to parse html form array with Beego.
As you can see from the implementation of the FormValue method of the Request, it returns the first value in case of multiple ones: http://golang.org/src/pkg/net/http/request.go?s=23078:23124#L795 It would be better to get the attribute itself r.Form[key] and iterate over all the results manually. I am not sure how Beego works, but just using the raw Request.ParseForm and Request.Form or Request.PostForm maps should do the job. http://golang.org/src/pkg/net/http/request.go?s=1939:6442#L61
Thanks @ysqi for giving me a hint. I am adding a little detailed example to parse associate array like form data in beego
Here is my form structure:
<input name="contacts[0][email]" type="text" value="a1@gmail.com"/>
<input name="contacts[0][first_name]" type="text" value="f1"/>
<input name="contacts[0][last_name]" type="text" value="l1"/>
<input name="contacts[1][email]" type="text" value="a2@gmail.com"/>
<input name="contacts[1][first_name]" type="text" value="f2"/>
<input name="contacts[1][last_name]" type="text" value="l2"/>
golang(beego) code:
contacts := make([]map[string]string, 0, 3)
this.Ctx.Input.Bind(&contacts, "contacts")
contacts variable:
[
{
"email": "user2@gmail.com",
"first_name": "Sam",
"last_name": "Gamge"
},
{
"email": "user3@gmail.com",
"first_name": "john",
"last_name": "doe"
}
]
Now you can use it like:
for _, contact := range contacts {
contact["email"]
contact["first_name"]
contact["last_name"]
}
You can do like this : see doc
v := make([]string, 0, 3)
this.Ctx.Input.Bind(&v, "names")