Are there a possibility to bind post form values from array of objects using gin?
I have post form like
<input type="text" name="class">
<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
And go code
type Student struct {
First string `form:"first"`
Last string `form:"last"`
Age int `form:"age"`
}
type Class struct {
Class string `form:"class"`
Students []Student `form:"students[]"`
}
func Handle(c *gin.Context) {
err := c.Request.ParseForm()
if err != nil {
log.Fatal(err)
}
class:= new(Class)
err = c.Bind(class)
if err != nil {
log.Fatal(err)
}
log.Println(c.Request.PostForm)
log.Println(class)
}
I receive next output
map[students[][first]:[John Jack] students[][last]:[Johny Jacky] students[][age]:[20 30] class:[myclass]]
&{myclass []}
But I expect to receive array of structs Student and get output like
&{myclass [{John Johny 20} {Jack Jacky 30}]}
How is it possible to do it?
Similar issue
https://github.com/gin-gonic/gin/issues/1523
Unfortunately it is not possible yet.
same question,hope support
Can't wait to see this supported! Coming from PHP, things like this were very common.