Skip to content

Commit

Permalink
Removed unneeded form interface method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestefanello committed Feb 24, 2025
1 parent 149005c commit ab7be40
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 57 deletions.
25 changes: 11 additions & 14 deletions pkg/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,34 @@ import (
"github.com/mikestefanello/pagoda/pkg/context"
)

// Form represents a form that can be submitted and validated
// Form represents a form that can be submitted and validated.
type Form interface {
// Submit marks the form as submitted, stores a pointer to it in the context, binds the request
// values to the struct fields, and validates the input based on the struct tags.
// Returns a validator.ValidationErrors if the form values were not valid.
// Returns an echo.HTTPError if the request failed to process.
Submit(c echo.Context, form any) error

// IsSubmitted returns true if the form was submitted
// IsSubmitted returns true if the form was submitted.
IsSubmitted() bool

// IsValid returns true if the form has no validation errors
// IsValid returns true if the form has no validation errors.
IsValid() bool

// IsDone returns true if the form was submitted and has no validation errors
// IsDone returns true if the form was submitted and has no validation errors.
IsDone() bool

// FieldHasErrors returns true if a given struct field has validation errors
// FieldHasErrors returns true if a given struct field has validation errors.
FieldHasErrors(fieldName string) bool

// SetFieldError sets a validation error message for a given struct field
// SetFieldError sets a validation error message for a given struct field.
SetFieldError(fieldName string, message string)

// GetFieldErrors returns the validation errors for a given struct field
// GetFieldErrors returns the validation errors for a given struct field.
GetFieldErrors(fieldName string) []string

// GetFieldStatusClass returns a CSS class to be used for a given struct field
GetFieldStatusClass(fieldName string) string
}

// Get gets a form from the context or initializes a new copy if one is not set
// Get gets a form from the context or initializes a new copy if one is not set.
func Get[T any](ctx echo.Context) *T {
if v := ctx.Get(context.FormKey); v != nil {
return v.(*T)
Expand All @@ -44,13 +41,13 @@ func Get[T any](ctx echo.Context) *T {
return &v
}

// Clear removes the form set in the context
// Clear removes the form set in the context.
func Clear(ctx echo.Context) {
ctx.Set(context.FormKey, nil)
}

// Submit submits a form
// See Form.Submit()
// Submit submits a form.
// See Form.Submit().
func Submit(ctx echo.Context, form Form) error {
return form.Submit(ctx, form)
}
28 changes: 9 additions & 19 deletions pkg/form/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ import (
// Submission represents the state of the submission of a form, not including the form itself.
// This satisfies the Form interface.
type Submission struct {
// isSubmitted indicates if the form has been submitted
// isSubmitted indicates if the form has been submitted.
isSubmitted bool

// errors stores a slice of error message strings keyed by form struct field name
// errors stores a slice of error message strings keyed by form struct field name.
errors map[string][]string
}

func (f *Submission) Submit(ctx echo.Context, form any) error {
f.isSubmitted = true

// Set in context so the form can later be retrieved
// Set in context so the form can later be retrieved.
ctx.Set(context.FormKey, form)

// Bind the values from the incoming request to the form struct
// Bind the values from the incoming request to the form struct.
if err := ctx.Bind(form); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("unable to bind form: %v", err))
}

// Validate the form
// Validate the form.
if err := ctx.Validate(form); err != nil {
f.setErrorMessages(err)
return err
Expand Down Expand Up @@ -73,17 +73,7 @@ func (f *Submission) GetFieldErrors(fieldName string) []string {
return f.errors[fieldName]
}

func (f *Submission) GetFieldStatusClass(fieldName string) string {
if f.isSubmitted {
if f.FieldHasErrors(fieldName) {
return "is-danger"
}
return "is-success"
}
return ""
}

// setErrorMessages sets errors messages on the submission for all fields that failed validation
// setErrorMessages sets errors messages on the submission for all fields that failed validation.
func (f *Submission) setErrorMessages(err error) {
// Only this is supported right now
ves, ok := err.(validator.ValidationErrors)
Expand All @@ -94,8 +84,8 @@ func (f *Submission) setErrorMessages(err error) {
for _, ve := range ves {
var message string

// Provide better error messages depending on the failed validation tag
// This should be expanded as you use additional tags in your validation
// Provide better error messages depending on the failed validation tag.
// This should be expanded as you use additional tags in your validation.
switch ve.Tag() {
case "required":
message = "This field is required."
Expand All @@ -109,7 +99,7 @@ func (f *Submission) setErrorMessages(err error) {
message = "Invalid value."
}

// Add the error
// Add the error.
f.SetFieldError(ve.Field(), message)
}
}
2 changes: 0 additions & 2 deletions pkg/form/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func TestFormSubmission(t *testing.T) {
require.Len(t, form.GetFieldErrors("Name"), 1)
assert.Len(t, form.GetFieldErrors("Email"), 0)
assert.Equal(t, "This field is required.", form.GetFieldErrors("Name")[0])
assert.Equal(t, "is-danger", form.GetFieldStatusClass("Name"))
assert.Equal(t, "is-success", form.GetFieldStatusClass("Email"))
assert.False(t, form.IsDone())

formInCtx := Get[formTest](ctx)
Expand Down
8 changes: 3 additions & 5 deletions pkg/handlers/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (
"github.com/mikestefanello/pagoda/pkg/ui"
)

type (
Contact struct {
mail *services.MailClient
}
)
type Contact struct {
mail *services.MailClient
}

func init() {
Register(new(Contact))
Expand Down
3 changes: 2 additions & 1 deletion pkg/handlers/pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"net/http"
"testing"

"github.com/mikestefanello/pagoda/pkg/routenames"
"github.com/stretchr/testify/assert"
)

// Simple example of how to test routes and their markup using the test HTTP server spun up within
// this test package
func TestPages__About(t *testing.T) {
doc := request(t).
setRoute(routeNameAbout).
setRoute(routenames.About).
get().
assertStatusCode(http.StatusOK).
toDoc()
Expand Down
4 changes: 1 addition & 3 deletions pkg/handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/ui"
)

type (
Search struct{}
)
type Search struct{}

func init() {
Register(new(Search))
Expand Down
8 changes: 3 additions & 5 deletions pkg/handlers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import (
"github.com/mikestefanello/pagoda/pkg/tasks"
)

type (
Task struct {
tasks *backlite.Client
}
)
type Task struct {
tasks *backlite.Client
}

func init() {
Register(new(Task))
Expand Down
16 changes: 8 additions & 8 deletions pkg/ui/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,22 @@ func sidebarMenu(r *request) Node {
Ul(
Class("menu-list"),
menuLink(r, "Dashboard", routenames.Home),
menuLink(r, "About", "about"),
menuLink(r, "About", routenames.About),
menuLink(r, "Contact", routenames.Contact),
menuLink(r, "Cache", "cache"),
menuLink(r, "Task", "task"),
menuLink(r, "Files", "files"),
menuLink(r, "Cache", routenames.Cache),
menuLink(r, "Task", routenames.Task),
menuLink(r, "Files", routenames.Files),
),
P(
Class("menu-label"),
Text("Account"),
),
Ul(
Class("menu-list"),
If(r.IsAuth, menuLink(r, "Logout", "logout")),
If(!r.IsAuth, menuLink(r, "Login", "login")),
If(!r.IsAuth, menuLink(r, "Register", "register")),
If(!r.IsAuth, menuLink(r, "Forgot password", "forgot_password")),
If(r.IsAuth, menuLink(r, "Logout", routenames.Logout)),
If(!r.IsAuth, menuLink(r, "Login", routenames.Login)),
If(!r.IsAuth, menuLink(r, "Register", routenames.Register)),
If(!r.IsAuth, menuLink(r, "Forgot password", routenames.ForgotPasswordSubmit)),
),
)
}
Expand Down

0 comments on commit ab7be40

Please sign in to comment.