2024-06-09 18:39:04 -07:00
|
|
|
package form
|
2021-12-22 20:40:08 -08:00
|
|
|
|
|
|
|
import (
|
2024-06-14 09:35:35 -07:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-22 20:40:08 -08:00
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/context"
|
2021-12-22 20:40:08 -08:00
|
|
|
)
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
// Submission represents the state of the submission of a form, not including the form itself.
|
|
|
|
// This satisfies the Form interface.
|
2024-06-09 18:39:04 -07:00
|
|
|
type Submission struct {
|
2024-06-14 09:35:35 -07:00
|
|
|
// isSubmitted indicates if the form has been submitted
|
|
|
|
isSubmitted bool
|
2021-12-22 20:40:08 -08:00
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
// errors stores a slice of error message strings keyed by form struct field name
|
|
|
|
errors map[string][]string
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
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))
|
|
|
|
}
|
2021-12-22 20:40:08 -08:00
|
|
|
|
|
|
|
// Validate the form
|
|
|
|
if err := ctx.Validate(form); err != nil {
|
2021-12-24 21:11:59 -08:00
|
|
|
f.setErrorMessages(err)
|
2024-06-14 09:35:35 -07:00
|
|
|
return err
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
func (f *Submission) IsSubmitted() bool {
|
|
|
|
return f.isSubmitted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Submission) IsValid() bool {
|
|
|
|
if f.errors == nil {
|
|
|
|
return true
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
2024-06-14 09:35:35 -07:00
|
|
|
return len(f.errors) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Submission) IsDone() bool {
|
|
|
|
return f.IsSubmitted() && f.IsValid()
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
func (f *Submission) FieldHasErrors(fieldName string) bool {
|
2021-12-22 20:40:08 -08:00
|
|
|
return len(f.GetFieldErrors(fieldName)) > 0
|
|
|
|
}
|
|
|
|
|
2024-06-09 18:39:04 -07:00
|
|
|
func (f *Submission) SetFieldError(fieldName string, message string) {
|
2024-06-14 09:35:35 -07:00
|
|
|
if f.errors == nil {
|
|
|
|
f.errors = make(map[string][]string)
|
2021-12-23 05:57:27 -08:00
|
|
|
}
|
2024-06-14 09:35:35 -07:00
|
|
|
f.errors[fieldName] = append(f.errors[fieldName], message)
|
2021-12-23 05:57:27 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
func (f *Submission) GetFieldErrors(fieldName string) []string {
|
|
|
|
if f.errors == nil {
|
2021-12-22 20:40:08 -08:00
|
|
|
return []string{}
|
|
|
|
}
|
2024-06-14 09:35:35 -07:00
|
|
|
return f.errors[fieldName]
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
func (f *Submission) GetFieldStatusClass(fieldName string) string {
|
|
|
|
if f.isSubmitted {
|
2021-12-23 05:57:27 -08:00
|
|
|
if f.FieldHasErrors(fieldName) {
|
|
|
|
return "is-danger"
|
|
|
|
}
|
|
|
|
return "is-success"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:11:59 -08:00
|
|
|
// setErrorMessages sets errors messages on the submission for all fields that failed validation
|
2024-06-09 18:39:04 -07:00
|
|
|
func (f *Submission) setErrorMessages(err error) {
|
2021-12-22 20:40:08 -08:00
|
|
|
// Only this is supported right now
|
|
|
|
ves, ok := err.(validator.ValidationErrors)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
switch ve.Tag() {
|
|
|
|
case "required":
|
|
|
|
message = "This field is required."
|
|
|
|
case "email":
|
|
|
|
message = "Enter a valid email address."
|
|
|
|
case "eqfield":
|
|
|
|
message = "Does not match."
|
2024-06-22 07:34:26 -07:00
|
|
|
case "gte":
|
|
|
|
message = fmt.Sprintf("Must be greater than or equal to %v.", ve.Param())
|
2021-12-22 20:40:08 -08:00
|
|
|
default:
|
|
|
|
message = "Invalid value."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the error
|
2021-12-23 20:04:00 -08:00
|
|
|
f.SetFieldError(ve.Field(), message)
|
2021-12-22 20:40:08 -08:00
|
|
|
}
|
|
|
|
}
|