表单验证支持设置默认值
This commit is contained in:
@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user