Problem/Motivation
Required fields in Form API have both the required="required" attribute as well as the aria-required="true" attribute.
- This is flagged in Siteimprove as a WCAG Level A warning, with the following generic text:
A WAI-ARIA attribute that has the exact same features as the HTML element it has been applied to has been used. The WAI-ARIA attribute is redundant since is doesn't provide the user with any additional information.
For landmarks it has previously been a recommendation to use HTML5 and WAI-ARIA landmark roles together (e.g. WAI-ARIA role="navigation" on HTML5 'nav' elements) to maximize support, but with the widespread adoption of HTML5 this is no longer needed.
Where to find it in WCAG 2:
4 Robust > 4.1 Compatible > A > 4.1.2 Name, Role, Value > Warning > Redundant WAI-ARIA attribute - According to On HTML belts and ARIA braces (The Default Implicit ARIA semantics they didn’t want you to know about) by Steve Faulkner — one of the editors of the ARIA spec — adding the 'aria-required' in addition to the native HTML 'required' attribute is a, quote, “waste of time” since it is already set by the browser, as can be seen in https://stevefaulkner.github.io/html-mapping-tests .
Steps to reproduce
Inspect a node edit screen and look at any required field, such as node title.
Proposed resolution
Siteimprove suggests the following fix:
The WAI-ARIA attribute can be removed without any impact for end users. The result will be cleaner, easier to maintain code.
The suggested changes would affect Drupal Core in these places:
setAttributesmethod of core/lib/Drupal/Core/Render/Element/RenderElement.php:
// This function is invoked from form element theme functions, but the // rendered form element may not necessarily have been processed by // \Drupal::formBuilder()->doBuildForm(). if (!empty($element['#required'])) { $element['#attributes']['class'][] = 'required'; $element['#attributes']['required'] = 'required'; $element['#attributes']['aria-required'] = 'true'; #<-- HERE }$document.on('state:required'of core/misc/states.es6.js.
if (e.value) { const label = `label${e.target.id ? `[for=${e.target.id}]` : ''}`; const $label = $(e.target) .attr({ required: 'required', 'aria-required': 'true' }) /*<-- HERE */ .closest('.js-form-item, .js-form-wrapper') .find(label); // Avoids duplicate required markers on initialization. if (!$label.hasClass('js-form-required').length) { $label.addClass('js-form-required form-required'); } } else { $(e.target) .removeAttr('required aria-required') /*<-- HERE */ .closest('.js-form-item, .js-form-wrapper') .find('label.js-form-required') .removeClass('js-form-required form-required'); }
The addition of aria-required in addition to the required attribute was done over 10 years ago in #1174938: Natively support the HTML5 required and aria-required FAPI properties.
Remaining tasks
- Validate that this is an actual bug or a red herring from Siteimprove.
- If bug, write a patch with tests.
- If red herring, write to Siteimprove to fix their tests.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #49 | 3096790-49.patch | 8.63 KB | mohammed motar |
| #45 | 3096790-45-10.1.patch | 8.37 KB | mrshowerman |
| #30 | interdiff_20-30.txt | 1.78 KB | mrshowerman |
| #30 | 3096790-30-9.5.patch | 9.3 KB | mrshowerman |
| #20 | interdiff_18-20.txt | 2.56 KB | danflanagan8 |
Issue fork drupal-3096790
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3096790-aria-required-attribute-is
changes, plain diff MR !8514
Comments
Comment #5
glugmeister commentedThe W3C HTML validator gives a warning where form elements have both required and aria-required attributes: “Attribute aria-required is unnecessary for elements that have attribute required“. As it is a warning and not an error, I'm not sure whether it must be fixed to achieve compliance with W3C WCAG (https://www.w3.org/TR/UNDERSTANDING-WCAG20/ensure-compat-parses.html) - but perhaps best to fix anyway. All browsers support the native HTML5 required attribute - the aria-required attribute is no longer needed unless your supporting very old browsers like IE9 (https://caniuse.com/mdn-api_htmlinputelement_required).
Comment #6
mrshowermanHere's a first patch that removes all occurrences of
aria-required, except for ckeditor.js, which is a 3rd party library and should be handled elsewhere.Comment #7
mrshowermanChanged states.js manually by mistake in #6
Comment #8
duaelfrI just closed #3240052: redundant wai-aria attribute for aria-required as a duplicate of this issue.
Please credit @jwilson3 for their amazing work on the issue summary that you might want to copy here.
Comment #9
jwilson3I've in merged the issue summary and tags from #3240052: redundant wai-aria attribute for aria-required. Thanks @DuaelFr
Probably it could even be updated with feedback from comment #1 above as well.
Comment #10
jwilson3Comment #11
SomeIntern commentedManually tested with 9.4 and can confirm the patch applied and worked. Setting to RTBC.
Comment #12
SomeIntern commentedReverting metadata changed by mistake.
Comment #13
damienmckennaIt seems like this was duplicated in #3213473, which also has test coverage. Maybe the two should be combined?
Comment #14
mrshowerman#3213473: Checkbox input can't have aria-required attribute is about removing the attribute just from the checkbox field, whereas this issue attempts to remove it entirely from all input fields.
MDN states that aria-required should be used for backwards compatibility only. I think we should check compatibility of Drupal's supported browsers, and then either close #3213473 and add test coverage here, or close this one here.
Comment #15
swirtI agree that #7 would be a better solution than what is being done in https://www.drupal.org/project/drupal/issues/3213473
Comment #16
netboss commentedJust applied and tested #7 and it's working fine for us. And I also agree with swirt, this patch should be used instead.
Comment #17
swirtJust clarifying the testing here. We are now getting passing AXE tests on required fieldset, checkbox, and checkboxes that were formerly failing accessibility scans due to applying `aria-required=true` on these three elements that do not support it.
Regarding tests for this particular change, it seems odd to add a test for something that is completely being removed from the codebase. It is not as though conditional logic was added that needs to be tested, the patch in #7 flat out removes addition of the attribute. I suggest changing this status to RTBC
Comment #18
danflanagan8That Needs tests tag still gives me pause. Test coverage is fairly easy to add for this. And since one could reasonably argue this is a bug, we certainly don't want regression in the future.
Here's a patch that just adds a bunch of simple assertions to the existing FunctionalJavascript tests for the #states api. And then I added a unit test for the
RenderElement::setAttributesfunction. I couldn't find any existing unit test coverage for that function so I ended up adding cases that aren't directly related to this issue. They're simple and they show that we're not accidentally breaking thesetAttributesfunction somehow.Comment #20
danflanagan8Those failures look perfect so I'm removing the Needs tests tag.
Here's another patch where the fix from #7 runs against the new tests.
Comment #24
mohammed motar commentedThe patch in #20 works well in our environment
Comment #25
mohammed motar commentedThe patch in #20 works well in our environment.
Comment #26
Manoj Raj.R commented#20 looks good to me.
+1 RTBC
Comment #27
gaurav-mathur commentedPatch #20 applied successfully on drupal version 9 and working fine.
+1 RTBC
Comment #28
mgiffordThis looks like a good change. We should default to HTML & not have redundant ARIA.
Comment #29
jwilson3Comment #30
mrshowermanRe-rolling for 9.5.x and 10.1.x.
Comment #31
mgiffordTagged for 4.1.2.
Comment #32
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Tested this out on Drupal10.1.x with a standard install
Added a required field to the Basic page content type
Verified the required + the aria-required field
Applied the fix
Verified I just now see the required attribute
Running the tests without the fix
Changes look good to me.
Comment #33
xjmThanks everyone for working on this!
Crediting @thatguy for the initial issue report, @mrshowerman and @danflanagan8 for work on the patch, @jwilson3 for the duplicate, @DuaelFr for triage, @mgifford for accessibility review, and @smustgrave, @DamienMcKenna, and @Glugmeister for further review.
For others: The automated testing infrastructure tells us whether the change set still applies, so we do not need people to review that. It is also not sufficient criteria for the issue to be marked "Reviewed and Tested by the Community".
What we do need people to review is whether the issue has a correct scope, whether it passes the core gates, whether the solution completely fixes the problem without introducing other problems, and whether it's the best solution we can come up with. See the patch review guide for more information.
When you do post a review, be sure to describe what you reviewed and how (rather than just "it works as expected" or "+1" or similar statements). This helps other reviewers understand why you considered the issue RTBC (and is considered for issue credit).
Also see the issue credit guidelines for more information on which kinds of contributions are credited.
Comment #34
xjmThis should have parameter documentation for the parameters from the data provider, and parameter and return typehints.
The standard format for this is "Data provider for testSetAttributes()." It should also have return value documentation with type
array[], and a return typehint. The return documentation can be something like:Since this changes markup as well as the user interface for assistive tech users, it normally would not be backported to a patch release. However, since it's an accessibility bug (i.e., telling the user that the element is required twice), it may be eligible for backport. Quoting @Glugmeister:
As far as I can tell from the linked chart, the only thing that's unclear is whether or not UC Browser supports it. UC Browser was supported by 9.5.x, but support for it was removed in Drupal 10. That would be the only reason not to backport this -- but I'm not sure it's sufficient reason not to backport it.
Tagging for accessibility maintainer review of whether it is okay to backport this to 10.0.x and 9.5.x. I considered FEFM signoff, but in this case I think the accessibility aspect is the main thing that's important.
Comment #41
xjmI closed #3213473: Checkbox input can't have aria-required attribute as a duplicate and am adding credits from that issue here.
Comment #43
xjmComment #44
mgifford@xjm I don't think we need to backport issues that fall under WCAG SC 4.1.1. There's a big debate about removing it from WCAG 2.2, so it may not be in WCAG's future.
The thing is that most parsing errors aren't a problem for accessibility. When 4.1.1 was written, there was some assistive technology that worked with HTML source code. Now they all use the DOM or Accessibility Tree, both of which have been cleaned up by the browser.
So we're an organization that supports open standards, so it is good to fix parsing errors, but it may not make sense to see them as accessibility issues.
I tagged this with WCAG SC 4.1.2 Name, Role, Value.
Redundancy isn't good, but not sure if it needs backporting. It's a pretty low impact issue.
I have to say I also had trouble finding this reference "Where to find it in WCAG 2: 4 Robust > 4.1 Compatible > A > 4.1.2 Name, Role, Value > Warning > Redundant WAI-ARIA attribute" from the project descrption. Not sure why a link wasn't given.
Comment #45
mrshowermanRe-roll against current 10.1.x. Leaving in NW as per #34.
Comment #46
bnjmnmIt is OK to backport this change to 10.0.x and 9.5.x, but as mentioned in in #44 it is also not imperative that this happens.
Comment #49
mohammed motar commentedI have created a patch file from the latest commit in the merge request.
Comment #50
patricia_zoocha commentedPatch #49 applied successfully on Drupal version 10.3.6 and working fine.
Comment #51
mgiffordComment #52
smustgrave commentedleft some comments on the MR.
Comment #54
dcam commentedFeedback was addressed or responded to.
Comment #55
smustgrave commentedBeen about a month and don't want to hold it up, was hoping someone else would pick up the review.
Did verify this issue just by using the title field on a content type, verified both the required and aria-required
After the MR I just see the required attribute
Already did a code review
Believe this is ready for committer eyes.
Comment #58
nod_Committed bf98f85 and pushed to 11.x. Thanks!
Comment #60
liam morlandWill this be backported to Drupal 10? There is old discussion above about backporting but no decision.