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
|
|
@ -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