Using go and gin-gonic, I'd like to post a simple form containing two tag fields and then save it to mongodb.
Here is the form:
<form action="/quotes/{{ .quote.Id.Hex }}" method="POST">
<input type="text" name="author" value="{{ .quote.Author }}">
<textarea name="body" rows="3">{{ .quote.Body }}</textarea>
<input name="tag" value="" >
<input name="tag" value="" >
<button type="submit">Submit</button>
</form>
And the handler is:
func Create(c *gin.Context) {
db := c.MustGet("db").(*mgo.Database)
quote := models.Quote{}
err := c.Bind("e)
if err != nil {
c.Error(err)
return
}
//To debug
fmt.Println("form post values
")
for t, v := range c.Request.Form["tag"] {
fmt.Println(t, v)
}
//To debug
fmt.Println(quote)
err = db.C(models.CollectionQuote).Insert(quote)
if err != nil {
c.Error(err)
}
c.Redirect(http.StatusMovedPermanently, "/quotes")
}
Now the problem is as for form post values
I get:
0 mytag1
1 mytag2
and quote details
yields something like:
{ObjectIdHex("") some-author somebody [] }
The quote model is like this:
// Quote model
type Quote struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Author string `json:"author" form:"author" binding:"required" bson:"author"`
Body string `json:"body" form:"body" binding:"required" bson:"body"`
Tag []string `json:"tag" bson:"tag"`
}
So the tag values are received but not binded. I'm wondeirng how can I fix this and get the tags from the form? I've looked at the gin guid but could not find anything about this sort of forms.