Add support for better validation error messages.

This commit is contained in:
mikestefanello 2021-12-14 20:54:47 -05:00
parent b7f5704b2b
commit dee7a13cba
2 changed files with 33 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http"
"path"
"path/filepath"
"reflect"
"runtime"
"sync"
@ -14,6 +15,9 @@ import (
"goweb/container"
"goweb/funcmap"
"goweb/middleware"
"goweb/msg"
"github.com/go-playground/validator/v10"
"github.com/eko/gocache/v2/marshaler"
@ -151,6 +155,32 @@ func (t *Controller) Redirect(c echo.Context, route string, routeParams ...inter
return c.Redirect(http.StatusFound, c.Echo().Reverse(route, routeParams))
}
func (t *Controller) SetValidationErrorMessages(c echo.Context, err error, data interface{}) {
for _, ve := range err.(validator.ValidationErrors) {
var message string
// Default the field label to the name of the struct field
label := ve.StructField()
// Attempt to get a label from the field's struct tag
if field, ok := reflect.TypeOf(data).FieldByName(ve.Field()); ok {
if labelTag := field.Tag.Get("label"); labelTag != "" {
label = labelTag
}
}
// Provide better error messages depending on the failed validation tag
switch ve.Tag() {
case "required":
message = "%s is required."
default:
message = "%s is not a valid value."
}
msg.Danger(c, fmt.Sprintf(message, label))
}
}
// getTemplatesDirectoryPath gets the templates directory path
// This is needed incase this is called from a package outside of main,
// such as testing

View File

@ -19,8 +19,8 @@ type (
}
LoginForm struct {
Username string `form:"username" validate:"required"`
Password string `form:"password" validate:"required"`
Username string `form:"username" validate:"required" label:"Username"`
Password string `form:"password" validate:"required" label:"Password"`
}
)
@ -47,7 +47,7 @@ func (l *Login) Post(c echo.Context) error {
// Validate the form
if err := c.Validate(l.form); err != nil {
msg.Danger(c, "All fields are required.")
l.SetValidationErrorMessages(c, err, l.form)
return l.Get(c)
}