From e868f91f84cea3a76c621080a26ae39e998814c9 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 11 Sep 2024 12:21:58 +0200 Subject: [PATCH] Show form errors --- server/router/handler/user.go | 19 ++++++++--- server/router/pages/user.templ | 62 +++++++++++++++++++++------------- types/types.go | 4 +++ 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/server/router/handler/user.go b/server/router/handler/user.go index 30347a3..33acf35 100644 --- a/server/router/handler/user.go +++ b/server/router/handler/user.go @@ -3,6 +3,7 @@ package handler import ( "fmt" "net/http" + "regexp" "git.ekzyis.com/ekzyis/delphi.market/server/router/context" "git.ekzyis.com/ekzyis/delphi.market/server/router/pages" @@ -13,7 +14,7 @@ import ( func HandleUser(sc context.Context) echo.HandlerFunc { return func(c echo.Context) error { u := c.Get("session").(types.User) - return pages.User(&u).Render(context.RenderContext(sc, c), c.Response().Writer) + return pages.User(&u, nil).Render(context.RenderContext(sc, c), c.Response().Writer) } } @@ -26,15 +27,25 @@ func HandleUserEdit(sc context.Context) echo.HandlerFunc { name = c.FormValue("name") maxLength = 16 + errors types.UserEditError err error ) if name == "" { - return echo.NewHTTPError(http.StatusBadRequest, "name is required") + errors.Name = "required" } if len(name) > maxLength { - echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("name cannot be longer than %d characters", maxLength)) + errors.Name = fmt.Sprintf("%d characters too long", len(name)-maxLength) + } + + if !regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(name) { + errors.Name = "only letters, numbers, _ and - allowed" + } + + if errors.Name != "" { + c.Response().WriteHeader(http.StatusBadRequest) + return pages.User(&u, &errors).Render(context.RenderContext(sc, c), c.Response().Writer) } if err = db.QueryRowContext(ctx, @@ -43,6 +54,6 @@ func HandleUserEdit(sc context.Context) echo.HandlerFunc { return err } - return pages.User(&u).Render(context.RenderContext(sc, c), c.Response().Writer) + return pages.User(&u, &errors).Render(context.RenderContext(sc, c), c.Response().Writer) } } diff --git a/server/router/pages/user.templ b/server/router/pages/user.templ index 513c49e..5f1a793 100644 --- a/server/router/pages/user.templ +++ b/server/router/pages/user.templ @@ -1,13 +1,14 @@ package pages import ( + "fmt" "git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components" "git.ekzyis.com/ekzyis/delphi.market/types" "strconv" "time" ) -templ User(user *types.User) { +templ User(user *types.User, errors *types.UserEditError) { @components.Head() @@ -25,27 +26,33 @@ templ User(user *types.User) {
id
{ strconv.Itoa(user.Id) }
name
-
- { user.Name } - - - +
+
+ { user.Name } + + + +
+ if errors != nil && errors.Name != "" { +
{ errors.Name }
+ } else { + + }
joined
{ user.CreatedAt.Format(time.DateOnly) }
@@ -69,10 +76,19 @@ templ User(user *types.User) { htmx.on("#edit", "click", toggleForm) htmx.on("#cancel", "click", toggleForm) - htmx.on("form", "htmx:afterRequest", toggleForm) + htmx.on("form", "htmx:afterRequest", ({ detail }) => { + if (detail.successful) toggleForm() + })
@components.Footer() } + +func maybeShow(class string, show bool) string { + if show { + return class + } + return fmt.Sprintf("%s %s", class, "hidden") +} diff --git a/types/types.go b/types/types.go index e5cbead..6b32ba3 100644 --- a/types/types.go +++ b/types/types.go @@ -15,6 +15,10 @@ type User struct { Msats int64 } +type UserEditError struct { + Name string +} + type Invoice struct { Id int UserId int