Support datetime-local form element.
This commit is contained in:
parent
13ae07ae60
commit
cb5c3ad127
3 changed files with 29 additions and 12 deletions
|
|
@ -15,6 +15,8 @@ import (
|
|||
"github.com/mikestefanello/pagoda/ent/user"
|
||||
)
|
||||
|
||||
const dateTimeFormat = "2006-01-02T15:04:05"
|
||||
|
||||
type Handler struct {
|
||||
client *ent.Client
|
||||
Config HandlerConfig
|
||||
|
|
@ -175,7 +177,7 @@ func (h *Handler) PasswordTokenGet(ctx echo.Context, id int) (url.Values, error)
|
|||
|
||||
v := url.Values{}
|
||||
v.Set("user_id", fmt.Sprint(entity.UserID))
|
||||
v.Set("created_at", entity.CreatedAt.Format(time.RFC3339))
|
||||
v.Set("created_at", entity.CreatedAt.Format(dateTimeFormat))
|
||||
return v, err
|
||||
}
|
||||
|
||||
|
|
@ -289,11 +291,19 @@ func (h *Handler) getPageAndOffset(ctx echo.Context) (int, int) {
|
|||
}
|
||||
|
||||
func (h *Handler) bind(ctx echo.Context, entity any) error {
|
||||
// Echo requires some pre-processing of form values to avoid problems.
|
||||
for k, v := range ctx.Request().Form {
|
||||
// Remove empty field values so Echo's bind does not 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)
|
||||
continue
|
||||
}
|
||||
|
||||
// Echo expects datetime values to be in a certain format but that does not align with the datetime-local
|
||||
// HTML form element format, so we will attempt to convert it here.
|
||||
if t, err := time.Parse(dateTimeFormat, v[0]); err == nil {
|
||||
ctx.Request().Form[k][0] = t.Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
return ctx.Bind(entity)
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@
|
|||
{{- end }}
|
||||
)
|
||||
|
||||
const dateTimeFormat = "2006-01-02T15:04:05"
|
||||
|
||||
type Handler struct {
|
||||
client *{{ $pkg }}.Client
|
||||
Config HandlerConfig
|
||||
|
|
@ -215,7 +217,7 @@
|
|||
{{- if eq $f.Type.String "string" }}
|
||||
v.Set("{{ $f.Name }}", entity.{{ fieldName $f.Name }})
|
||||
{{- else if eq $f.Type.String "time.Time" }}
|
||||
v.Set("{{ $f.Name }}", entity.{{ fieldName $f.Name }}.Format(time.RFC3339))
|
||||
v.Set("{{ $f.Name }}", entity.{{ fieldName $f.Name }}.Format(dateTimeFormat))
|
||||
{{- else }}
|
||||
v.Set("{{ $f.Name }}", fmt.Sprint(entity.{{ fieldName $f.Name }}))
|
||||
{{- end }}
|
||||
|
|
@ -235,11 +237,19 @@
|
|||
}
|
||||
|
||||
func (h *Handler) bind(ctx echo.Context, entity any) error {
|
||||
// Echo requires some pre-processing of form values to avoid problems.
|
||||
for k, v := range ctx.Request().Form {
|
||||
// Remove empty field values so Echo's bind does not 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)
|
||||
continue
|
||||
}
|
||||
|
||||
// Echo expects datetime values to be in a certain format but that does not align with the datetime-local
|
||||
// HTML form element format, so we will attempt to convert it here.
|
||||
if t, err := time.Parse(dateTimeFormat, v[0]); err == nil {
|
||||
ctx.Request().Form[k][0] = t.Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
return ctx.Bind(entity)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/entc/gen"
|
||||
"entgo.io/ent/entc/load"
|
||||
|
|
@ -88,12 +87,10 @@ func AdminEntityForm(ctx echo.Context, isNew bool, schema *load.Schema, values u
|
|||
}
|
||||
nodes = append(nodes, InputField(p))
|
||||
case field.TypeTime:
|
||||
// todo make this easier
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: f.Name,
|
||||
InputType: "text",
|
||||
InputType: "datetime-local",
|
||||
Label: admin.FieldLabel(f.Name),
|
||||
Help: fmt.Sprintf("Use the following format: %s", time.Now().Format(time.RFC3339)),
|
||||
Value: getValue(f.Name),
|
||||
}))
|
||||
case field.TypeInt, field.TypeInt8, field.TypeInt16, field.TypeInt32, field.TypeInt64,
|
||||
|
|
@ -179,7 +176,7 @@ func AdminEntityList(ctx echo.Context, params AdminEntityListParams) error {
|
|||
r.Path(routenames.AdminEntityEdit(params.EntityType.Name), row.ID),
|
||||
"is-link",
|
||||
"Edit",
|
||||
),
|
||||
), // todo make this easier
|
||||
),
|
||||
Td(
|
||||
ButtonLink(r.Path(routenames.AdminEntityDelete(params.EntityType.Name), row.ID),
|
||||
|
|
@ -210,7 +207,7 @@ func AdminEntityList(ctx echo.Context, params AdminEntityListParams) error {
|
|||
return r.Render(layouts.Admin, Group{
|
||||
ButtonLink(
|
||||
r.Path(routenames.AdminEntityAdd(params.EntityType.Name)),
|
||||
"is-link",
|
||||
"is-primary",
|
||||
fmt.Sprintf("Add %s", params.EntityType.Name),
|
||||
),
|
||||
Table(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue