I have this code:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
type TestForm struct {
Age int `form:"age" binding:"required"`
Name string `form:"name" binding:"required"`
}
func home(c *gin.Context) {
c.HTML(200, "index.html", nil)
}
func homePost(c *gin.Context) {
var f TestForm
err := c.Bind(&f)
c.String(200, fmt.Sprintf("%v : %v", err, f))
}
func main() {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Use(gin.Recovery())
r.GET("/", home)
r.POST("/", homePost)
r.Run()
}
and templates/index.html :
<!doctype html>
<html>
<head></head>
<body>
<h1>hello</h1>
<form action="/" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
When the fields fail binding/validation, I would like to iterate over all the fields in order to render an error in HTML on the matching field.
If I input test as name
leave age
blank, the response is this:
Key: 'TestForm.Age' Error:Field validation for 'Age' failed on the 'required' tag : {0 asd}
I have found the err
returned from err := c.Bind(&f)
to be the type validator.ValidationErrors
which allows me to do exactly this, so far all is good.
However if I input test as name
and abc
as age` (or eve just input a number with a space after it), the response is this:
strconv.ParseInt: parsing "abc": invalid syntax : {0 }
So, just a generic error that somewhere something failed to parse an integer.
Now I have no way of knowing which field failed, nor do I get any info about other fields failing validation. Is there any way to have gin tell me that the age
field failed in this case ?