Initial commit of form submission struct.
This commit is contained in:
parent
b61077dac9
commit
57159c4fba
5 changed files with 126 additions and 21 deletions
91
controller/form.go
Normal file
91
controller/form.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type FormSubmission struct {
|
||||
IsSubmitted bool
|
||||
|
||||
Errors map[string][]string
|
||||
}
|
||||
|
||||
func (f *FormSubmission) Process(ctx echo.Context, form interface{}) error {
|
||||
f.Errors = make(map[string][]string)
|
||||
f.IsSubmitted = true
|
||||
|
||||
// Validate the form
|
||||
if err := ctx.Validate(form); err != nil {
|
||||
f.setErrorMessages(form, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FormSubmission) HasErrors() bool {
|
||||
if f.Errors == nil {
|
||||
return false
|
||||
}
|
||||
return len(f.Errors) > 0
|
||||
}
|
||||
|
||||
func (f FormSubmission) FieldHasError(fieldName string) bool {
|
||||
return len(f.GetFieldErrors(fieldName)) > 0
|
||||
}
|
||||
|
||||
func (f FormSubmission) GetFieldErrors(fieldName string) []string {
|
||||
if f.Errors == nil {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
errors, has := f.Errors[fieldName]
|
||||
if !has {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
|
||||
// Only this is supported right now
|
||||
ves, ok := err.(validator.ValidationErrors)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
formType := reflect.TypeOf(form)
|
||||
|
||||
for _, ve := range ves {
|
||||
var message string
|
||||
|
||||
// Default the field form name to the name of the struct field
|
||||
fieldName := ve.StructField()
|
||||
|
||||
// Attempt to get the form field name from the field's struct tag
|
||||
if field, ok := formType.FieldByName(ve.Field()); ok {
|
||||
if fieldNameTag := field.Tag.Get("form"); fieldNameTag != "" {
|
||||
fieldName = fieldNameTag
|
||||
}
|
||||
}
|
||||
|
||||
// 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."
|
||||
default:
|
||||
message = "Invalid value."
|
||||
}
|
||||
|
||||
// Add the error
|
||||
f.Errors[fieldName] = append(f.Errors[fieldName], message)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue