From d6ef2b0b73fed63a936b8e17983d491fe754c38b Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Thu, 23 Dec 2021 08:57:27 -0500 Subject: [PATCH] Added form field statuses during validation. --- controller/form.go | 21 +++++++++++++++++++-- controller/htmx.go | 3 ++- controller/page.go | 6 ++++++ routes/contact.go | 6 +++--- templates/components/forms.gohtml | 6 +++--- templates/pages/contact.gohtml | 8 ++++---- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/controller/form.go b/controller/form.go index dbf9078..014ba1b 100644 --- a/controller/form.go +++ b/controller/form.go @@ -33,10 +33,17 @@ func (f FormSubmission) HasErrors() bool { return len(f.Errors) > 0 } -func (f FormSubmission) FieldHasError(fieldName string) bool { +func (f FormSubmission) FieldHasErrors(fieldName string) bool { return len(f.GetFieldErrors(fieldName)) > 0 } +func (f *FormSubmission) SetFieldError(fieldName string, message string) { + if f.Errors == nil { + f.Errors = make(map[string][]string) + } + f.Errors[fieldName] = append(f.Errors[fieldName], message) +} + func (f FormSubmission) GetFieldErrors(fieldName string) []string { if f.Errors == nil { return []string{} @@ -50,6 +57,16 @@ func (f FormSubmission) GetFieldErrors(fieldName string) []string { return errors } +func (f FormSubmission) GetFieldStatusClass(fieldName string) string { + if f.IsSubmitted { + if f.FieldHasErrors(fieldName) { + return "is-danger" + } + return "is-success" + } + return "" +} + func (f *FormSubmission) setErrorMessages(form interface{}, err error) { // Only this is supported right now ves, ok := err.(validator.ValidationErrors) @@ -86,6 +103,6 @@ func (f *FormSubmission) setErrorMessages(form interface{}, err error) { } // Add the error - f.Errors[fieldName] = append(f.Errors[fieldName], message) + f.SetFieldError(fieldName, message) } } diff --git a/controller/htmx.go b/controller/htmx.go index 1ed8461..ff684aa 100644 --- a/controller/htmx.go +++ b/controller/htmx.go @@ -34,10 +34,11 @@ type ( Trigger string TriggerAfterSwap string TriggerAfterSettle string + // TODO: No content 204 response? } ) -func NewHTMXRequest(ctx echo.Context) HTMXRequest { +func GetHTMXRequest(ctx echo.Context) HTMXRequest { return HTMXRequest{ Enabled: ctx.Request().Header.Get(HTMXHeaderRequest) == "true", Trigger: ctx.Request().Header.Get(HTMXHeaderTrigger), diff --git a/controller/page.go b/controller/page.go index df324d0..3e0783b 100644 --- a/controller/page.go +++ b/controller/page.go @@ -44,6 +44,12 @@ type Page struct { // This is what the controller uses to pass the content of the page. Data interface{} + // Form stores a struct that represents a form on the page. + // This should be a struct with fields for each form field, using both "form" and "validate" tags + // It should also contain a Submission field of type FormSubmission if you wish to have validation + // messagesa and markup presented to the user + Form interface{} + // Layout stores the name of the layout base template file which will be used when the page is rendered. // This should match a template file located within the layouts directory inside the templates directory. // The template extension should not be included in this value. diff --git a/routes/contact.go b/routes/contact.go index 343c8da..fcaf5ed 100644 --- a/routes/contact.go +++ b/routes/contact.go @@ -27,10 +27,10 @@ func (c *Contact) Get(ctx echo.Context) error { p.Layout = "main" p.Name = "contact" p.Title = "Contact us" - p.Data = ContactForm{} + p.Form = ContactForm{} if form := ctx.Get(context.FormKey); form != nil { - p.Data = form.(ContactForm) + p.Form = form.(ContactForm) } return c.RenderPage(ctx, p) @@ -59,7 +59,7 @@ func (c *Contact) Post(ctx echo.Context) error { return c.Get(ctx) } - htmx := controller.NewHTMXRequest(ctx) + htmx := controller.GetHTMXRequest(ctx) if htmx.Enabled { return ctx.String(http.StatusOK, "HELLO!") diff --git a/templates/components/forms.gohtml b/templates/components/forms.gohtml index fba4442..d67e716 100644 --- a/templates/components/forms.gohtml +++ b/templates/components/forms.gohtml @@ -2,8 +2,8 @@ {{end}} -{{define "form-field-errors"}} - {{range .}} +{{define "field-errors"}} + {{- range .}}

{{.}}

- {{end}} + {{- end}} {{end}} \ No newline at end of file diff --git a/templates/pages/contact.gohtml b/templates/pages/contact.gohtml index 14070cb..1514808 100644 --- a/templates/pages/contact.gohtml +++ b/templates/pages/contact.gohtml @@ -3,17 +3,17 @@
- +
- {{template "form-field-errors" (.Data.Submission.GetFieldErrors "email")}} + {{template "field-errors" (.Form.Submission.GetFieldErrors "email")}}
- +
- {{template "form-field-errors" (.Data.Submission.GetFieldErrors "message")}} + {{template "field-errors" (.Form.Submission.GetFieldErrors "message")}}