Initial commit of form submission struct.
This commit is contained in:
parent
b61077dac9
commit
57159c4fba
5 changed files with 126 additions and 21 deletions
|
|
@ -16,8 +16,9 @@ type (
|
|||
}
|
||||
|
||||
ContactForm struct {
|
||||
Email string `form:"email" validate:"required,email" label:"Email address"`
|
||||
Message string `form:"message" validate:"required" label:"Message"`
|
||||
Email string `form:"email" validate:"required,email"`
|
||||
Message string `form:"message" validate:"required"`
|
||||
Submission controller.FormSubmission
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -36,28 +37,31 @@ func (c *Contact) Get(ctx echo.Context) error {
|
|||
}
|
||||
|
||||
func (c *Contact) Post(ctx echo.Context) error {
|
||||
fail := func(message string, err error) error {
|
||||
ctx.Logger().Errorf("%s: %v", message, err)
|
||||
msg.Danger(ctx, "An error occurred. Please try again.")
|
||||
return c.Get(ctx)
|
||||
}
|
||||
//fail := func(message string, err error) error {
|
||||
// ctx.Logger().Errorf("%s: %v", message, err)
|
||||
// msg.Danger(ctx, "An error occurred. Please try again.")
|
||||
// return c.Get(ctx)
|
||||
//}
|
||||
|
||||
// Parse the form values
|
||||
var form ContactForm
|
||||
if err := ctx.Bind(&form); err != nil {
|
||||
return fail("unable to parse contact form", err)
|
||||
ctx.Logger().Error(err)
|
||||
}
|
||||
|
||||
if err := form.Submission.Process(ctx, form); err != nil {
|
||||
// TOOD
|
||||
}
|
||||
|
||||
ctx.Set(context.FormKey, form)
|
||||
|
||||
// Validate the form
|
||||
if err := ctx.Validate(form); err != nil {
|
||||
c.SetValidationErrorMessages(ctx, err, form)
|
||||
if form.Submission.HasErrors() {
|
||||
return c.Get(ctx)
|
||||
}
|
||||
|
||||
p := controller.NewHTMX(ctx)
|
||||
htmx := controller.NewHTMXRequest(ctx)
|
||||
|
||||
if p.Request.Enabled {
|
||||
if htmx.Enabled {
|
||||
return ctx.String(http.StatusOK, "<b>HELLO!</b>")
|
||||
} else {
|
||||
msg.Success(ctx, "Thank you for contacting us!")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue