-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I get map[string]string filed on PostForm? #1362
Comments
Not sure, but maybe something like this
|
ShouldBindonly support struct type. |
Now I just write a loop and get the value by PostForm, then change to the map struct by myself, but I think gin should auto handle this map struct
|
please post some example code and why must use |
For example, multi language
name[zh-CN] = '姓名'
name[en] = 'Name'
the map key must be pass, else, you never know what language.
It is common struct for php or another languages.
I test it, golang framework iris can handle this case , and
https://github.com/go-playground/form also.
I think gin should handle this case, right?
Currently, gin bind form is not working, I need do this for every post form
before
for _, value := range languages {
form.Name[value] = c.PostForm("name[" + value + "]")
}
|
Yes, I see it.
But I think we should consider more situation, because map is not only map string.That’s why I have not implement by myself.
|
@syssam Yes, but I think we should not do complex, as far as possible simple. In my option, if you use complex map in query string or post form, maybe you should use custom struct or json. |
Just idea, how about this to use more flexible? type QueryString string
// these methods are copied from json.Number
func (s QueryString) String() string { return string(s) }
func (s QueryString) Float64() (float64, error) {
return strconv.ParseFloat(string(s), 64)
}
func (s QueryString) Int64() (int64, error) {
return strconv.ParseInt(string(s), 10, 64)
}
dict := map[string]QueryString
... and usage dict := c.QueryMap("ids")
dict["a"].String() // "1234"
dict["a"].Int64() // 1234, nil |
#1383 have supported map parameters for query string and post form. |
not support form mapping bind now. |
@thinkerou Ya.
|
@syssam please read #1387 comment, and you checkout my branch https://github.com/thinkerou/gin/tree/support-map-for-map-form |
@thinkerou sorry for the late reply.Please make a html form, you will find out the problem. |
@syssam your mean is what use |
Just like
|
Please try the latest version and see the document. https://github.com/gin-gonic/gin#map-as-querystring-or-postform-parameters |
I was using gorm library to update models using a map(like db.Model(&user).Updates(map[string]interface{}{"uname": "hello", "age": 18, "actived": false})), and needed a bind method that binds request body to a map. I always use a gin context wrapper it has been good practice for me and developed below code and works well for my needs. That would be great if implemented directly in gin. It serves dynamic data binding specially for PUT method.
|
#1749 merged! |
How can I get map[string]string filed on PostForm?
The text was updated successfully, but these errors were encountered: