Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a11y problems with validation error messages generated by Bootstrap form_for #147

Merged
merged 1 commit into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion form/bootstrap/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bootstrap

import (
"fmt"
"strconv"
"strings"

"github.com/gobuffalo/flect"
Expand Down Expand Up @@ -109,12 +110,24 @@ func divWrapper(opts tags.Options, fn func(opts tags.Options) tags.Body) *tags.T
}

if hasErrors {
for _, err := range errors {
fieldID, _ := opts["id"].(string)
errorIDs := make([]string, 0, len(errors))
for i, err := range errors {
errID := "error-" + fieldID + "-" + strconv.Itoa(i)
div.Append(tags.New("div", tags.Options{
"class": "invalid-feedback help-block",
"body": err,
"id": errID,
}))
errorIDs = append(errorIDs, errID)
}
descBy, _ := opts["aria-describedby"].(string)
if descBy != "" {
descBy += " "
}
descBy += strings.Join(errorIDs, " ")
opts["aria-describedby"] = descBy
opts["aria-invalid"] = "true"
}

if hasHelp {
Expand Down
10 changes: 5 additions & 5 deletions form/bootstrap/form_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func Test_InputError_CustomError(t *testing.T) {

f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.InputTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block">My Custom Error</div></div>`, l.String())
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input aria-describedby="error--Name-0" aria-invalid="true" class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block" id="error--Name-0">My Custom Error</div></div>`, l.String())
}

func Test_InputError(t *testing.T) {
Expand All @@ -220,7 +220,7 @@ func Test_InputError(t *testing.T) {

f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.InputTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block">Name shoud be AJ.</div></div>`, l.String())
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input aria-describedby="error--Name-0" aria-invalid="true" class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block" id="error--Name-0">Name shoud be AJ.</div></div>`, l.String())
}

func Test_InputHidden(t *testing.T) {
Expand All @@ -246,7 +246,7 @@ func Test_InputError_Map(t *testing.T) {

f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.InputTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block">Name shoud be AJ.</div></div>`, l.String())
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input aria-describedby="error--Name-0" aria-invalid="true" class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block" id="error--Name-0">Name shoud be AJ.</div></div>`, l.String())
}

func Test_InputError_InvalidMap(t *testing.T) {
Expand All @@ -270,7 +270,7 @@ func Test_InputMultipleError(t *testing.T) {

f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.InputTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block">Name shoud be AJ.</div><div class="invalid-feedback help-block">Name shoud start with A.</div></div>`, l.String())
r.Equal(`<div class="form-group has-error"><label class="form-label" for="-Name">Custom</label><input aria-describedby="error--Name-0 error--Name-1" aria-invalid="true" class="form-control is-invalid" id="-Name" name="Name" type="text" value="" /><div class="invalid-feedback help-block" id="error--Name-0">Name shoud be AJ.</div><div class="invalid-feedback help-block" id="error--Name-1">Name shoud start with A.</div></div>`, l.String())
}

func Test_CheckBoxError(t *testing.T) {
Expand All @@ -281,7 +281,7 @@ func Test_CheckBoxError(t *testing.T) {

f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors})
l := f.CheckboxTag("Name", tags.Options{"label": "Custom"})
r.Equal(`<div class="form-check has-error"><input class="form-check-input is-invalid" id="-Name" name="Name" type="checkbox" value="true" /><label class="form-check-label" for="-Name">Custom</label><div class="invalid-feedback help-block">Name shoud be AJ.</div></div>`, l.String())
r.Equal(`<div class="form-check has-error"><input aria-describedby="error--Name-0" aria-invalid="true" class="form-check-input is-invalid" id="-Name" name="Name" type="checkbox" value="true" /><label class="form-check-label" for="-Name">Custom</label><div class="invalid-feedback help-block" id="error--Name-0">Name shoud be AJ.</div></div>`, l.String())
}

type Person struct {
Expand Down