Improve form and template usage (#66)
* Improve form and template usage.
This commit is contained in:
parent
5f66b0ee71
commit
97bef0257e
22 changed files with 341 additions and 274 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/mikestefanello/pagoda/ent"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/form"
|
||||
"github.com/mikestefanello/pagoda/pkg/htmx"
|
||||
"github.com/mikestefanello/pagoda/pkg/msg"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
|
@ -34,9 +35,6 @@ type Page struct {
|
|||
// Context stores the request context
|
||||
Context echo.Context
|
||||
|
||||
// ToURL is a function to convert a route name and optional route parameters to a URL
|
||||
ToURL func(name string, params ...any) string
|
||||
|
||||
// Path stores the path of the current request
|
||||
Path string
|
||||
|
||||
|
|
@ -50,8 +48,8 @@ type Page struct {
|
|||
// 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 any
|
||||
// messages and markup presented to the user
|
||||
Form form.Form
|
||||
|
||||
// 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.
|
||||
|
|
@ -67,7 +65,7 @@ type Page struct {
|
|||
// IsHome stores whether the requested page is the home page or not
|
||||
IsHome bool
|
||||
|
||||
// IsAuth stores whether or not the user is authenticated
|
||||
// IsAuth stores whether the user is authenticated
|
||||
IsAuth bool
|
||||
|
||||
// AuthUser stores the authenticated user
|
||||
|
|
@ -125,7 +123,6 @@ type Page struct {
|
|||
func NewPage(ctx echo.Context) Page {
|
||||
p := Page{
|
||||
Context: ctx,
|
||||
ToURL: ctx.Echo().Reverse,
|
||||
Path: ctx.Request().URL.Path,
|
||||
URL: ctx.Request().URL.String(),
|
||||
StatusCode: http.StatusOK,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ func TestNewPage(t *testing.T) {
|
|||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
p := NewPage(ctx)
|
||||
assert.Same(t, ctx, p.Context)
|
||||
assert.NotNil(t, p.ToURL)
|
||||
assert.Equal(t, "/", p.Path)
|
||||
assert.Equal(t, "/", p.URL)
|
||||
assert.Equal(t, http.StatusOK, p.StatusCode)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,40 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
)
|
||||
|
||||
// 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() bool
|
||||
|
||||
// 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() bool
|
||||
|
||||
// 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(fieldName string, message string)
|
||||
|
||||
// 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
|
||||
func Get[T any](ctx echo.Context) *T {
|
||||
if v := ctx.Get(context.FormKey); v != nil {
|
||||
|
|
@ -17,18 +44,13 @@ func Get[T any](ctx echo.Context) *T {
|
|||
return &v
|
||||
}
|
||||
|
||||
// Set sets a form in the context and binds the request values to it
|
||||
func Set(ctx echo.Context, form any) error {
|
||||
ctx.Set(context.FormKey, form)
|
||||
|
||||
if err := ctx.Bind(form); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("unable to bind form: %v", err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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()
|
||||
func Submit(ctx echo.Context, form Form) error {
|
||||
return form.Submit(ctx, form)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,34 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestContextFuncs(t *testing.T) {
|
||||
type mockForm struct {
|
||||
called bool
|
||||
Submission
|
||||
}
|
||||
|
||||
func (m *mockForm) Submit(_ echo.Context, _ any) error {
|
||||
m.called = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSubmit(t *testing.T) {
|
||||
m := mockForm{}
|
||||
ctx, _ := tests.NewContext(echo.New(), "/")
|
||||
err := Submit(ctx, &m)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, m.called)
|
||||
}
|
||||
|
||||
func TestGetClear(t *testing.T) {
|
||||
e := echo.New()
|
||||
|
||||
type example struct {
|
||||
|
|
@ -26,29 +42,17 @@ func TestContextFuncs(t *testing.T) {
|
|||
assert.NotNil(t, form)
|
||||
})
|
||||
|
||||
t.Run("set bad request", func(t *testing.T) {
|
||||
// Set with a bad request
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("abc=abc"))
|
||||
ctx := e.NewContext(req, httptest.NewRecorder())
|
||||
var form example
|
||||
err := Set(ctx, &form)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("set", func(t *testing.T) {
|
||||
// Set and parse the values
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("name=abc"))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
ctx := e.NewContext(req, httptest.NewRecorder())
|
||||
var form example
|
||||
err := Set(ctx, &form)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "abc", form.Name)
|
||||
t.Run("get non-empty context", func(t *testing.T) {
|
||||
form := example{
|
||||
Name: "test",
|
||||
}
|
||||
ctx, _ := tests.NewContext(e, "/")
|
||||
ctx.Set(context.FormKey, &form)
|
||||
|
||||
// Get again and expect the values were stored
|
||||
got := Get[example](ctx)
|
||||
require.NotNil(t, got)
|
||||
assert.Equal(t, "abc", form.Name)
|
||||
assert.Equal(t, "test", form.Name)
|
||||
|
||||
// Clear
|
||||
Clear(ctx)
|
||||
|
|
|
|||
|
|
@ -1,65 +1,80 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Submission represents the state of the submission of a form, not including the form itself
|
||||
// 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 bool
|
||||
// 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 map[string][]string
|
||||
// errors stores a slice of error message strings keyed by form struct field name
|
||||
errors map[string][]string
|
||||
}
|
||||
|
||||
// Process processes a submission for a form
|
||||
func (f *Submission) Process(ctx echo.Context, form any) error {
|
||||
f.Errors = make(map[string][]string)
|
||||
f.IsSubmitted = true
|
||||
func (f *Submission) Submit(ctx echo.Context, form any) error {
|
||||
f.isSubmitted = true
|
||||
|
||||
// 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
|
||||
if err := ctx.Bind(form); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("unable to bind form: %v", err))
|
||||
}
|
||||
|
||||
// Validate the form
|
||||
if err := ctx.Validate(form); err != nil {
|
||||
f.setErrorMessages(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasErrors indicates if the submission has any validation errors
|
||||
func (f Submission) HasErrors() bool {
|
||||
if f.Errors == nil {
|
||||
return false
|
||||
}
|
||||
return len(f.Errors) > 0
|
||||
func (f *Submission) IsSubmitted() bool {
|
||||
return f.isSubmitted
|
||||
}
|
||||
|
||||
// FieldHasErrors indicates if a given field on the form has any validation errors
|
||||
func (f Submission) FieldHasErrors(fieldName string) bool {
|
||||
func (f *Submission) IsValid() bool {
|
||||
if f.errors == nil {
|
||||
return true
|
||||
}
|
||||
return len(f.errors) == 0
|
||||
}
|
||||
|
||||
func (f *Submission) IsDone() bool {
|
||||
return f.IsSubmitted() && f.IsValid()
|
||||
}
|
||||
|
||||
func (f *Submission) FieldHasErrors(fieldName string) bool {
|
||||
return len(f.GetFieldErrors(fieldName)) > 0
|
||||
}
|
||||
|
||||
// SetFieldError sets an error message for a given field name
|
||||
func (f *Submission) SetFieldError(fieldName string, message string) {
|
||||
if f.Errors == nil {
|
||||
f.Errors = make(map[string][]string)
|
||||
if f.errors == nil {
|
||||
f.errors = make(map[string][]string)
|
||||
}
|
||||
f.Errors[fieldName] = append(f.Errors[fieldName], message)
|
||||
f.errors[fieldName] = append(f.errors[fieldName], message)
|
||||
}
|
||||
|
||||
// GetFieldErrors gets the errors for a given field name
|
||||
func (f Submission) GetFieldErrors(fieldName string) []string {
|
||||
if f.Errors == nil {
|
||||
func (f *Submission) GetFieldErrors(fieldName string) []string {
|
||||
if f.errors == nil {
|
||||
return []string{}
|
||||
}
|
||||
return f.Errors[fieldName]
|
||||
return f.errors[fieldName]
|
||||
}
|
||||
|
||||
// GetFieldStatusClass returns an HTML class based on the status of the field
|
||||
func (f Submission) GetFieldStatusClass(fieldName string) string {
|
||||
if f.IsSubmitted {
|
||||
func (f *Submission) GetFieldStatusClass(fieldName string) string {
|
||||
if f.isSubmitted {
|
||||
if f.FieldHasErrors(fieldName) {
|
||||
return "is-danger"
|
||||
}
|
||||
|
|
@ -68,12 +83,6 @@ func (f Submission) GetFieldStatusClass(fieldName string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// IsDone indicates if the submission is considered done which is when it has been submitted
|
||||
// and there are no errors.
|
||||
func (f Submission) IsDone() bool {
|
||||
return f.IsSubmitted && !f.HasErrors()
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
|
|||
|
|
@ -1,40 +1,59 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/services"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFormSubmission(t *testing.T) {
|
||||
type formTest struct {
|
||||
Name string `validate:"required"`
|
||||
Email string `validate:"required,email"`
|
||||
Submission Submission
|
||||
Name string `form:"name" validate:"required"`
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
Submission
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Validator = services.NewValidator()
|
||||
ctx, _ := tests.NewContext(e, "/")
|
||||
form := formTest{
|
||||
Name: "",
|
||||
Email: "a@a.com",
|
||||
}
|
||||
err := form.Submission.Process(ctx, form)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, form.Submission.HasErrors())
|
||||
assert.True(t, form.Submission.FieldHasErrors("Name"))
|
||||
assert.False(t, form.Submission.FieldHasErrors("Email"))
|
||||
require.Len(t, form.Submission.GetFieldErrors("Name"), 1)
|
||||
assert.Len(t, form.Submission.GetFieldErrors("Email"), 0)
|
||||
assert.Equal(t, "This field is required.", form.Submission.GetFieldErrors("Name")[0])
|
||||
assert.Equal(t, "is-danger", form.Submission.GetFieldStatusClass("Name"))
|
||||
assert.Equal(t, "is-success", form.Submission.GetFieldStatusClass("Email"))
|
||||
assert.False(t, form.Submission.IsDone())
|
||||
t.Run("valid request", func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("email=a@a.com"))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
ctx := e.NewContext(req, httptest.NewRecorder())
|
||||
|
||||
var form formTest
|
||||
err := form.Submit(ctx, &form)
|
||||
assert.IsType(t, validator.ValidationErrors{}, err)
|
||||
|
||||
assert.Empty(t, form.Name)
|
||||
assert.Equal(t, "a@a.com", form.Email)
|
||||
assert.False(t, form.IsValid())
|
||||
assert.True(t, form.FieldHasErrors("Name"))
|
||||
assert.False(t, form.FieldHasErrors("Email"))
|
||||
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)
|
||||
require.NotNil(t, formInCtx)
|
||||
assert.Equal(t, form.Email, formInCtx.Email)
|
||||
})
|
||||
|
||||
t.Run("invalid request", func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("abc=abc"))
|
||||
ctx := e.NewContext(req, httptest.NewRecorder())
|
||||
var form formTest
|
||||
err := form.Submit(ctx, &form)
|
||||
assert.IsType(t, new(echo.HTTPError), err)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ import (
|
|||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/gommon/random"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -17,29 +17,28 @@ var (
|
|||
CacheBuster = random.String(10)
|
||||
)
|
||||
|
||||
// GetFuncMap provides a template function map
|
||||
func GetFuncMap() template.FuncMap {
|
||||
// See http://masterminds.github.io/sprig/ for available funcs
|
||||
funcMap := sprig.FuncMap()
|
||||
|
||||
// Provide a list of custom functions
|
||||
// Expand this as you add more functions to this package
|
||||
// Avoid using a name already in use by sprig
|
||||
f := template.FuncMap{
|
||||
"hasField": HasField,
|
||||
"file": File,
|
||||
"link": Link,
|
||||
}
|
||||
|
||||
for k, v := range f {
|
||||
funcMap[k] = v
|
||||
}
|
||||
|
||||
return funcMap
|
||||
type funcMap struct {
|
||||
web *echo.Echo
|
||||
}
|
||||
|
||||
// HasField checks if an interface contains a given field
|
||||
func HasField(v any, name string) bool {
|
||||
// NewFuncMap provides a template function map
|
||||
func NewFuncMap(web *echo.Echo) template.FuncMap {
|
||||
fm := &funcMap{web: web}
|
||||
|
||||
// See http://masterminds.github.io/sprig/ for all provided funcs
|
||||
funcs := sprig.FuncMap()
|
||||
|
||||
// Include all the custom functions
|
||||
funcs["hasField"] = fm.hasField
|
||||
funcs["file"] = fm.file
|
||||
funcs["link"] = fm.link
|
||||
funcs["url"] = fm.url
|
||||
|
||||
return funcs
|
||||
}
|
||||
|
||||
// hasField checks if an interface contains a given field
|
||||
func (fm *funcMap) hasField(v any, name string) bool {
|
||||
rv := reflect.ValueOf(v)
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
|
|
@ -50,13 +49,13 @@ func HasField(v any, name string) bool {
|
|||
return rv.FieldByName(name).IsValid()
|
||||
}
|
||||
|
||||
// File appends a cache buster to a given filepath so it can remain cached until the app is restarted
|
||||
func File(filepath string) string {
|
||||
// file appends a cache buster to a given filepath so it can remain cached until the app is restarted
|
||||
func (fm *funcMap) file(filepath string) string {
|
||||
return fmt.Sprintf("/%s/%s?v=%s", config.StaticPrefix, filepath, CacheBuster)
|
||||
}
|
||||
|
||||
// Link outputs HTML for a link element, providing the ability to dynamically set the active class
|
||||
func Link(url, text, currentPath string, classes ...string) template.HTML {
|
||||
// link outputs HTML for a link element, providing the ability to dynamically set the active class
|
||||
func (fm *funcMap) link(url, text, currentPath string, classes ...string) template.HTML {
|
||||
if currentPath == url {
|
||||
classes = append(classes, "is-active")
|
||||
}
|
||||
|
|
@ -64,3 +63,8 @@ func Link(url, text, currentPath string, classes ...string) template.HTML {
|
|||
html := fmt.Sprintf(`<a class="%s" href="%s">%s</a>`, strings.Join(classes, " "), url, text)
|
||||
return template.HTML(html)
|
||||
}
|
||||
|
||||
// url generates a URL from a given route name and optional parameters
|
||||
func (fm *funcMap) url(routeName string, params ...any) string {
|
||||
return fm.web.Reverse(routeName, params...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,36 +4,60 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewFuncMap(t *testing.T) {
|
||||
f := NewFuncMap(echo.New())
|
||||
assert.NotNil(t, f["hasField"])
|
||||
assert.NotNil(t, f["link"])
|
||||
assert.NotNil(t, f["file"])
|
||||
assert.NotNil(t, f["url"])
|
||||
}
|
||||
|
||||
func TestHasField(t *testing.T) {
|
||||
type example struct {
|
||||
name string
|
||||
}
|
||||
var e example
|
||||
assert.True(t, HasField(e, "name"))
|
||||
assert.False(t, HasField(e, "abcd"))
|
||||
f := new(funcMap)
|
||||
assert.True(t, f.hasField(e, "name"))
|
||||
assert.False(t, f.hasField(e, "abcd"))
|
||||
}
|
||||
|
||||
func TestLink(t *testing.T) {
|
||||
link := string(Link("/abc", "Text", "/abc"))
|
||||
f := new(funcMap)
|
||||
|
||||
link := string(f.link("/abc", "Text", "/abc"))
|
||||
expected := `<a class="is-active" href="/abc">Text</a>`
|
||||
assert.Equal(t, expected, link)
|
||||
|
||||
link = string(Link("/abc", "Text", "/abc", "first", "second"))
|
||||
link = string(f.link("/abc", "Text", "/abc", "first", "second"))
|
||||
expected = `<a class="first second is-active" href="/abc">Text</a>`
|
||||
assert.Equal(t, expected, link)
|
||||
|
||||
link = string(Link("/abc", "Text", "/def"))
|
||||
link = string(f.link("/abc", "Text", "/def"))
|
||||
expected = `<a class="" href="/abc">Text</a>`
|
||||
assert.Equal(t, expected, link)
|
||||
}
|
||||
|
||||
func TestFile(t *testing.T) {
|
||||
file := File("test.png")
|
||||
f := new(funcMap)
|
||||
|
||||
file := f.file("test.png")
|
||||
expected := fmt.Sprintf("/%s/test.png?v=%s", config.StaticPrefix, CacheBuster)
|
||||
assert.Equal(t, expected, file)
|
||||
}
|
||||
|
||||
func TestUrl(t *testing.T) {
|
||||
f := new(funcMap)
|
||||
f.web = echo.New()
|
||||
f.web.GET("/mypath/:id", func(c echo.Context) error {
|
||||
return nil
|
||||
}).Name = "test"
|
||||
out := f.url("test", 5)
|
||||
assert.Equal(t, "/mypath/5", out)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/ent"
|
||||
"github.com/mikestefanello/pagoda/ent/user"
|
||||
|
|
@ -38,14 +39,14 @@ type (
|
|||
}
|
||||
|
||||
forgotPasswordForm struct {
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
Submission form.Submission
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
form.Submission
|
||||
}
|
||||
|
||||
loginForm struct {
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
Password string `form:"password" validate:"required"`
|
||||
Submission form.Submission
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
Password string `form:"password" validate:"required"`
|
||||
form.Submission
|
||||
}
|
||||
|
||||
registerForm struct {
|
||||
|
|
@ -53,13 +54,13 @@ type (
|
|||
Email string `form:"email" validate:"required,email"`
|
||||
Password string `form:"password" validate:"required"`
|
||||
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
|
||||
Submission form.Submission
|
||||
form.Submission
|
||||
}
|
||||
|
||||
resetPasswordForm struct {
|
||||
Password string `form:"password" validate:"required"`
|
||||
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
|
||||
Submission form.Submission
|
||||
form.Submission
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -114,17 +115,14 @@ func (c *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
|
|||
return c.ForgotPasswordPage(ctx)
|
||||
}
|
||||
|
||||
// Set the form in context and parse the form values
|
||||
if err := form.Set(ctx, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
err := form.Submit(ctx, &input)
|
||||
|
||||
if err := input.Submission.Process(ctx, input); err != nil {
|
||||
return c.Fail(err, "unable to process form submission")
|
||||
}
|
||||
|
||||
if input.Submission.HasErrors() {
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
case validator.ValidationErrors:
|
||||
return c.ForgotPasswordPage(ctx)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
|
||||
// Attempt to load the user
|
||||
|
|
@ -179,23 +177,20 @@ func (c *Auth) LoginSubmit(ctx echo.Context) error {
|
|||
var input loginForm
|
||||
|
||||
authFailed := func() error {
|
||||
input.Submission.SetFieldError("Email", "")
|
||||
input.Submission.SetFieldError("Password", "")
|
||||
input.SetFieldError("Email", "")
|
||||
input.SetFieldError("Password", "")
|
||||
msg.Danger(ctx, "Invalid credentials. Please try again.")
|
||||
return c.LoginPage(ctx)
|
||||
}
|
||||
|
||||
// Set in context and parse the form values
|
||||
if err := form.Set(ctx, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
err := form.Submit(ctx, &input)
|
||||
|
||||
if err := input.Submission.Process(ctx, input); err != nil {
|
||||
return c.Fail(err, "unable to process form submission")
|
||||
}
|
||||
|
||||
if input.Submission.HasErrors() {
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
case validator.ValidationErrors:
|
||||
return c.LoginPage(ctx)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
|
||||
// Attempt to load the user
|
||||
|
|
@ -250,17 +245,14 @@ func (c *Auth) RegisterPage(ctx echo.Context) error {
|
|||
func (c *Auth) RegisterSubmit(ctx echo.Context) error {
|
||||
var input registerForm
|
||||
|
||||
// Set in context and parse the form values
|
||||
if err := form.Set(ctx, &input); err != nil {
|
||||
return c.Fail(err, "unable to parse register form")
|
||||
}
|
||||
err := form.Submit(ctx, &input)
|
||||
|
||||
if err := input.Submission.Process(ctx, input); err != nil {
|
||||
return c.Fail(err, "unable to process form submission")
|
||||
}
|
||||
|
||||
if input.Submission.HasErrors() {
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
case validator.ValidationErrors:
|
||||
return c.RegisterPage(ctx)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
|
|
@ -341,17 +333,14 @@ func (c *Auth) ResetPasswordPage(ctx echo.Context) error {
|
|||
func (c *Auth) ResetPasswordSubmit(ctx echo.Context) error {
|
||||
var input resetPasswordForm
|
||||
|
||||
// Set in context and parse the form values
|
||||
if err := form.Set(ctx, &input); err != nil {
|
||||
return c.Fail(err, "unable to parse password reset form")
|
||||
}
|
||||
err := form.Submit(ctx, &input)
|
||||
|
||||
if err := input.Submission.Process(ctx, input); err != nil {
|
||||
return c.Fail(err, "unable to process form submission")
|
||||
}
|
||||
|
||||
if input.Submission.HasErrors() {
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
case validator.ValidationErrors:
|
||||
return c.ResetPasswordPage(ctx)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
|
||||
// Hash the new password
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package handlers
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/pkg/form"
|
||||
|
|
@ -25,7 +26,7 @@ type (
|
|||
Email string `form:"email" validate:"required,email"`
|
||||
Department string `form:"department" validate:"required,oneof=sales marketing hr"`
|
||||
Message string `form:"message" validate:"required"`
|
||||
Submission form.Submission
|
||||
form.Submission
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -57,26 +58,25 @@ func (c *Contact) Page(ctx echo.Context) error {
|
|||
func (c *Contact) Submit(ctx echo.Context) error {
|
||||
var input contactForm
|
||||
|
||||
// Store in context and parse the form values
|
||||
if err := form.Set(ctx, &input); err != nil {
|
||||
err := form.Submit(ctx, &input)
|
||||
|
||||
switch err.(type) {
|
||||
case nil:
|
||||
case validator.ValidationErrors:
|
||||
return c.Page(ctx)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
|
||||
if err := input.Submission.Process(ctx, input); err != nil {
|
||||
return c.Fail(err, "unable to process form submission")
|
||||
}
|
||||
err = c.mail.
|
||||
Compose().
|
||||
To(input.Email).
|
||||
Subject("Contact form submitted").
|
||||
Body(fmt.Sprintf("The message is: %s", input.Message)).
|
||||
Send(ctx)
|
||||
|
||||
if !input.Submission.HasErrors() {
|
||||
err := c.mail.
|
||||
Compose().
|
||||
To(input.Email).
|
||||
Subject("Contact form submitted").
|
||||
Body(fmt.Sprintf("The message is: %s", input.Message)).
|
||||
Send(ctx)
|
||||
|
||||
if err != nil {
|
||||
return c.Fail(err, "unable to send email")
|
||||
}
|
||||
if err != nil {
|
||||
return c.Fail(err, "unable to send email")
|
||||
}
|
||||
|
||||
return c.Page(ctx)
|
||||
|
|
|
|||
|
|
@ -48,8 +48,8 @@ func Danger(ctx echo.Context, message string) {
|
|||
Set(ctx, TypeDanger, message)
|
||||
}
|
||||
|
||||
// Set adds a new flash message of a given type into the session storage
|
||||
// Errors will logged and not returned
|
||||
// Set adds a new flash message of a given type into the session storage.
|
||||
// Errors will be logged and not returned.
|
||||
func Set(ctx echo.Context, typ Type, message string) {
|
||||
if sess, err := getSession(ctx); err == nil {
|
||||
sess.AddFlash(message, string(typ))
|
||||
|
|
@ -57,8 +57,8 @@ func Set(ctx echo.Context, typ Type, message string) {
|
|||
}
|
||||
}
|
||||
|
||||
// Get gets flash messages of a given type from the session storage
|
||||
// Errors will logged and not returned
|
||||
// Get gets flash messages of a given type from the session storage.
|
||||
// Errors will be logged and not returned.
|
||||
func Get(ctx echo.Context, typ Type) []string {
|
||||
var msgs []string
|
||||
|
||||
|
|
|
|||
|
|
@ -183,7 +183,7 @@ func (c *Container) initAuth() {
|
|||
|
||||
// initTemplateRenderer initializes the template renderer
|
||||
func (c *Container) initTemplateRenderer() {
|
||||
c.TemplateRenderer = NewTemplateRenderer(c.Config)
|
||||
c.TemplateRenderer = NewTemplateRenderer(c.Config, c.Web)
|
||||
}
|
||||
|
||||
// initMail initialize the mail client
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"io/fs"
|
||||
"sync"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/pkg/funcmap"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
|
@ -53,10 +54,10 @@ type (
|
|||
)
|
||||
|
||||
// NewTemplateRenderer creates a new TemplateRenderer
|
||||
func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
|
||||
func NewTemplateRenderer(cfg *config.Config, web *echo.Echo) *TemplateRenderer {
|
||||
return &TemplateRenderer{
|
||||
templateCache: sync.Map{},
|
||||
funcMap: funcmap.GetFuncMap(),
|
||||
funcMap: funcmap.NewFuncMap(web),
|
||||
config: cfg,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue