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

[5.x] Improve form field accessibility #10993

Open
wants to merge 20 commits into
base: 5.x
Choose a base branch
from

Conversation

daun
Copy link
Contributor

@daun daun commented Oct 22, 2024

There's a few requirements for accessible forms that make sense for built-in form fields. We just did at a11y audit on a client project and I figure I'd share what we learned with while the memory is fresh :)

A good primer on accessible forms best practices is Anatomy of Accessible Forms on deque. There's also a great summary about validation and marking invalid inputs in Contextual Form Errors and ARIA.

Partially closes statamic/ideas#1069

Changes

  • Associate form labels to their inputs via matching id and for attributes
  • Mark required fields: visually with an asterisk, as well as a Required label for screen readers
  • Render instructions for form fields
  • Link input and instructions via aria-describedby
  • Link input and validation error message via aria-describedby
  • Mark invalid inputs as aria-invalid

Next steps

There's two other things that make sense to tackle, but possibly in another PR to keep this one somewhat contained:

  • Wrap radios and checkboxes in a fieldset and legend
  • Allow rendering anchors to jump between error messages and the invalid fields they refer too

Before

<label>
  Name
</label>
<div>
  <input type="text" name="name" required>
</div>

After

<label for="contact-form-name-field">
  Name <sup aria-label="Required">*</sup>
</label>
<p id="contact-form-name-field-instructions">
  Enter your first and last name
</p>
<div>
  <input id="contact-form-name-field" type="text" name="name" required aria-describedby="contact-form-name-field-instructions">
</div>

@daun daun changed the title Improve form field accessibility [5.x] Improve form field accessibility Oct 22, 2024
@duncanmcclean
Copy link
Member

This looks awesome! 😄

@daun daun marked this pull request as ready for review October 25, 2024 09:14
@daun
Copy link
Contributor Author

daun commented Oct 25, 2024

Ready for review 🥬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve accessibility in prerendered form fields
2 participants