表单验证支持设置默认值

This commit is contained in:
2024-09-20 17:31:23 +08:00
parent df5a20ea87
commit 4f1e0e2649
3 changed files with 19 additions and 7 deletions

View File

@ -10,6 +10,7 @@ package request
import (
"bytes"
"errors"
"github.com/mcuadros/go-defaults"
"io"
"io/ioutil"
"net/http"
@ -56,14 +57,18 @@ func (f *form) Parse(ctx *gin.Context, receiver interface{}) error {
method == http.MethodPut ||
method == http.MethodDelete {
if ContentType.IsJson(ctx) {
return ctx.ShouldBindJSON(receiver)
if err := ctx.ShouldBindJSON(receiver); nil != err {
return err
}
} else if ContentType.IsFormURLEncoded(ctx) {
if err := ctx.ShouldBind(receiver); nil != err {
return err
}
} else {
return errors.New(ctx.ContentType() + " is not support")
}
if ContentType.IsFormURLEncoded(ctx) {
return ctx.ShouldBind(receiver)
}
return errors.New(ctx.ContentType() + " is not support")
}
// 设置默认值
defaults.SetDefaults(receiver)
return errors.New(method + " : request method is not support")
}