Added form field statuses during validation.
This commit is contained in:
parent
57159c4fba
commit
d6ef2b0b73
6 changed files with 37 additions and 13 deletions
|
|
@ -33,10 +33,17 @@ func (f FormSubmission) HasErrors() bool {
|
|||
return len(f.Errors) > 0
|
||||
}
|
||||
|
||||
func (f FormSubmission) FieldHasError(fieldName string) bool {
|
||||
func (f FormSubmission) FieldHasErrors(fieldName string) bool {
|
||||
return len(f.GetFieldErrors(fieldName)) > 0
|
||||
}
|
||||
|
||||
func (f *FormSubmission) SetFieldError(fieldName string, message string) {
|
||||
if f.Errors == nil {
|
||||
f.Errors = make(map[string][]string)
|
||||
}
|
||||
f.Errors[fieldName] = append(f.Errors[fieldName], message)
|
||||
}
|
||||
|
||||
func (f FormSubmission) GetFieldErrors(fieldName string) []string {
|
||||
if f.Errors == nil {
|
||||
return []string{}
|
||||
|
|
@ -50,6 +57,16 @@ func (f FormSubmission) GetFieldErrors(fieldName string) []string {
|
|||
return errors
|
||||
}
|
||||
|
||||
func (f FormSubmission) GetFieldStatusClass(fieldName string) string {
|
||||
if f.IsSubmitted {
|
||||
if f.FieldHasErrors(fieldName) {
|
||||
return "is-danger"
|
||||
}
|
||||
return "is-success"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
|
||||
// Only this is supported right now
|
||||
ves, ok := err.(validator.ValidationErrors)
|
||||
|
|
@ -86,6 +103,6 @@ func (f *FormSubmission) setErrorMessages(form interface{}, err error) {
|
|||
}
|
||||
|
||||
// Add the error
|
||||
f.Errors[fieldName] = append(f.Errors[fieldName], message)
|
||||
f.SetFieldError(fieldName, message)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,10 +34,11 @@ type (
|
|||
Trigger string
|
||||
TriggerAfterSwap string
|
||||
TriggerAfterSettle string
|
||||
// TODO: No content 204 response?
|
||||
}
|
||||
)
|
||||
|
||||
func NewHTMXRequest(ctx echo.Context) HTMXRequest {
|
||||
func GetHTMXRequest(ctx echo.Context) HTMXRequest {
|
||||
return HTMXRequest{
|
||||
Enabled: ctx.Request().Header.Get(HTMXHeaderRequest) == "true",
|
||||
Trigger: ctx.Request().Header.Get(HTMXHeaderTrigger),
|
||||
|
|
|
|||
|
|
@ -44,6 +44,12 @@ type Page struct {
|
|||
// This is what the controller uses to pass the content of the page.
|
||||
Data interface{}
|
||||
|
||||
// Form stores a struct that represents a form on the page.
|
||||
// This should be a struct with fields for each form field, using both "form" and "validate" tags
|
||||
// It should also contain a Submission field of type FormSubmission if you wish to have validation
|
||||
// messagesa and markup presented to the user
|
||||
Form interface{}
|
||||
|
||||
// Layout stores the name of the layout base template file which will be used when the page is rendered.
|
||||
// This should match a template file located within the layouts directory inside the templates directory.
|
||||
// The template extension should not be included in this value.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue