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
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue