Support multiple forms on a single page.
This commit is contained in:
parent
7505598324
commit
67a97832a5
2 changed files with 13 additions and 7 deletions
|
|
@ -9,8 +9,8 @@ import (
|
||||||
type Form interface {
|
type Form interface {
|
||||||
// Submit marks the form as submitted, stores a pointer to it in the context, binds the request
|
// 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.
|
// 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 a validator.ValidationErrors, if the form values were not valid, or an echo.HTTPError,
|
||||||
// Returns an echo.HTTPError if the request failed to process.
|
// if the request failed to process.
|
||||||
Submit(c echo.Context, form any) error
|
Submit(c echo.Context, form any) error
|
||||||
|
|
||||||
// IsSubmitted returns true if the form was submitted.
|
// IsSubmitted returns true if the form was submitted.
|
||||||
|
|
@ -35,7 +35,9 @@ type Form interface {
|
||||||
// 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 {
|
func Get[T any](ctx echo.Context) *T {
|
||||||
if v := ctx.Get(context.FormKey); v != nil {
|
if v := ctx.Get(context.FormKey); v != nil {
|
||||||
return v.(*T)
|
if form, ok := v.(*T); ok {
|
||||||
|
return form
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var v T
|
var v T
|
||||||
return &v
|
return &v
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ func TestGetClear(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("get empty context", func(t *testing.T) {
|
t.Run("get empty context", func(t *testing.T) {
|
||||||
// Empty context, still return a form
|
// Empty context, still return a form.
|
||||||
ctx, _ := tests.NewContext(e, "/")
|
ctx, _ := tests.NewContext(e, "/")
|
||||||
form := Get[example](ctx)
|
form := Get[example](ctx)
|
||||||
assert.NotNil(t, form)
|
assert.NotNil(t, form)
|
||||||
|
|
@ -49,12 +49,16 @@ func TestGetClear(t *testing.T) {
|
||||||
ctx, _ := tests.NewContext(e, "/")
|
ctx, _ := tests.NewContext(e, "/")
|
||||||
ctx.Set(context.FormKey, &form)
|
ctx.Set(context.FormKey, &form)
|
||||||
|
|
||||||
// Get again and expect the values were stored
|
// Get again and expect the values were stored.
|
||||||
got := Get[example](ctx)
|
got := Get[example](ctx)
|
||||||
require.NotNil(t, got)
|
require.NotNil(t, got)
|
||||||
assert.Equal(t, "test", form.Name)
|
assert.Equal(t, "test", got.Name)
|
||||||
|
|
||||||
// Clear
|
// Attempt getting a different type to ensure there's no panic.
|
||||||
|
ret := Get[int](ctx)
|
||||||
|
require.NotNil(t, ret)
|
||||||
|
|
||||||
|
// Clear.
|
||||||
Clear(ctx)
|
Clear(ctx)
|
||||||
got = Get[example](ctx)
|
got = Get[example](ctx)
|
||||||
require.NotNil(t, got)
|
require.NotNil(t, got)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue