-
Notifications
You must be signed in to change notification settings - Fork 8k
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
allow ignoring field on form mapping #1733
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1733 +/- ##
==========================================
+ Coverage 98.48% 98.48% +<.01%
==========================================
Files 41 41
Lines 2047 2049 +2
==========================================
+ Hits 2016 2018 +2
Misses 19 19
Partials 12 12
Continue to review full report at Codecov.
|
not
|
@thinkerou hey, thanks for your comment. problem is, as my comment here #610 (comment), gin uses binding for validation. That means the field still decoded by In your example, when printing |
its not working properly now. When I added form:"-" to my field, the field is still being assigned a value regardless of whether I use Bind or ShouldBind. type Astruct struct {
Aa string `form:"-" json:"aa"`
Bb *string `form:"-" json:"bb"`
}
func Init(r *gin.Engine) {
api := r.Group("/api")
{
api.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, "pong")
})
api.POST("/test", func(ctx *gin.Context) {
var a Astruct
ctx.Bind(&a)
fmt.Println(a.Aa)
fmt.Println(*a.Bb)
ctx.AbortWithStatus(http.StatusOK)
})
}
}
|
As discussed in issue #610 when using
c.Bind()
, sometimes we don't want to bind certain fields from the user request. While this can be done addingjson:"-"
to field tag, the same can't be achieved for multipart/formdata requests.This PR allows setting in the tag
form:"-"
, which solves the described issue.