Only bind form field values that are not empty.
This commit is contained in:
parent
271e252c53
commit
356cf21b3e
6 changed files with 42 additions and 13 deletions
|
|
@ -83,7 +83,7 @@ func (h *Handler) List(ctx echo.Context, entityType string) (*EntityList, error)
|
|||
|
||||
func (h *Handler) PasswordTokenCreate(ctx echo.Context) error {
|
||||
var payload PasswordToken
|
||||
if err := ctx.Bind(&payload); err != nil {
|
||||
if err := h.bind(ctx, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ func (h *Handler) PasswordTokenUpdate(ctx echo.Context, id int) error {
|
|||
}
|
||||
|
||||
var payload PasswordToken
|
||||
if err = ctx.Bind(&payload); err != nil {
|
||||
if err = h.bind(ctx, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -167,7 +167,7 @@ func (h *Handler) PasswordTokenGet(ctx echo.Context, id int) error {
|
|||
|
||||
func (h *Handler) UserCreate(ctx echo.Context) error {
|
||||
var payload User
|
||||
if err := ctx.Bind(&payload); err != nil {
|
||||
if err := h.bind(ctx, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -188,7 +188,7 @@ func (h *Handler) UserUpdate(ctx echo.Context, id int) error {
|
|||
}
|
||||
|
||||
var payload User
|
||||
if err = ctx.Bind(&payload); err != nil {
|
||||
if err = h.bind(ctx, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -262,3 +262,14 @@ func (h *Handler) getOffset(ctx echo.Context) int {
|
|||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (h *Handler) bind(ctx echo.Context, entity any) error {
|
||||
// Remove empty field values so Echo's bind does to fail when trying to parse things like
|
||||
// times, etc.
|
||||
for k, v := range ctx.Request().Form {
|
||||
if len(v) == 1 && len(v[0]) == 0 {
|
||||
delete(ctx.Request().Form, k)
|
||||
}
|
||||
}
|
||||
return ctx.Bind(entity)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@
|
|||
{{ range $n := $.Nodes }}
|
||||
func (h *Handler) {{ $n.Name }}Create(ctx echo.Context) error {
|
||||
var payload {{ $n.Name }}
|
||||
if err := ctx.Bind(&payload); err != nil {
|
||||
if err := h.bind(ctx, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
}
|
||||
|
||||
var payload {{ $n.Name }}
|
||||
if err = ctx.Bind(&payload); err != nil {
|
||||
if err = h.bind(ctx, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -204,4 +204,15 @@
|
|||
return 0
|
||||
}
|
||||
|
||||
func (h *Handler) bind(ctx echo.Context, entity any) error {
|
||||
// Remove empty field values so Echo's bind does to fail when trying to parse things like
|
||||
// times, etc.
|
||||
for k, v := range ctx.Request().Form {
|
||||
if len(v) == 1 && len(v[0]) == 0 {
|
||||
delete(ctx.Request().Form, k)
|
||||
}
|
||||
}
|
||||
return ctx.Bind(entity)
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
{{ range $n := $.Nodes }}
|
||||
type {{ $n.Name }} struct {
|
||||
{{- range $f := $n.Fields }}
|
||||
{{ fieldName $f.Name }} {{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
{{ fieldName $f.Name }} {{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue