Gin表单验证错误值为EOF
package forms
type PasswordLoginForm struct {
Mobile string `form:"mobile" json:"mobile" binding:"required"`
Password string `form:"password" json:"password" binding:"required,min=3,max=20"`
}
func PasswordLogin(ctx *gin.Context) {
passwordLoginForm := forms.PasswordLoginForm{}
if err := ctx.ShouldBind(&passwordLoginForm); err != nil {
color.Blue(err.Error())
ctx.JSON(http.StatusBadRequest, gin.H{
"msg": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "ok",
})
}
现在调试的时候不给手机号和密码这两个字段(body体为空)提示400错误,但是错误信息是EOF。
给这两个字段但不符合规则能正常提示;只给一个字段也能提示另一个字段需要required。
给这两个字段同时符合要求则返回ok。
passwordLoginForm := forms.PasswordLoginForm 就可以了,你赋值弄错了,宝贝!{ },这个是接口!
有找到问题吗?我本地都没有复现。不传body的时候,都显示failed on the 'required' tag