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

Merged
merged 22 commits into from
Dec 20, 2024

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 🥬

@jasonvarga
Copy link
Member

This is great, thanks. But just checking again about the changes in fields.antlers.html...

Since we don't seem to actually use that file anywhere, the user is responsible for making their field loop look like that in their own template. Right? Nothing will magically work - e.g. the aria-described-by doesn't link to anything unless they do it themselves.

Like you said, we can use that as a basic for the docs, or maybe in the future we can actually make it automatic.

@daun
Copy link
Contributor Author

daun commented Dec 19, 2024

@jasonvarga Right, seems like there‘s no automatic way of upgrading all forms to make use of the proposed updates. Users will need to update their loops as well.

Is it possible though that people are already using the fields partial in their own sites? In that case, it would work — and it would probably be a breaking change to remove it. Haven‘t come across any projects using it like this, though.

@jasonvarga
Copy link
Member

Actually, yes. That's a great idea too. Instead of the whole {{ fields }} ...etc... {{ /fields }} array we document, you could definitely just use that partial.

-{{ fields }}
-    ...
-{{ /fields }}
+{{ partial:statamic::forms.fields }}

@jasonvarga jasonvarga merged commit 33ba7cc into statamic:5.x Dec 20, 2024
18 checks passed
@daun daun deleted the feature/form-field-accessibility branch December 20, 2024 08:47
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
3 participants