Updated login form and controller.

This commit is contained in:
mikestefanello 2021-12-23 23:04:00 -05:00
parent 576caf217c
commit d5adf010db
6 changed files with 59 additions and 61 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http"
"reflect"
"goweb/htmx"
"goweb/middleware"
"goweb/msg"
"goweb/services"
@ -153,7 +154,11 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
// Redirect redirects to a given route name with optional route parameters
func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error {
return ctx.Redirect(http.StatusFound, ctx.Echo().Reverse(route, routeParams))
url := ctx.Echo().Reverse(route, routeParams)
h := htmx.Response{}
h.Redirect = url
h.Apply(ctx)
return ctx.Redirect(http.StatusFound, url)
}
func (c *Controller) Fail(ctx echo.Context, err error, log string) error {

View file

@ -1,8 +1,6 @@
package controller
import (
"reflect"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
@ -78,21 +76,9 @@ func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
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() {
@ -107,6 +93,6 @@ func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
}
// Add the error
f.SetFieldError(fieldName, message)
f.SetFieldError(ve.Field(), message)
}
}