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:

  1. setAttributes method 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
        }
    
  2. $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

  1. Validate that this is an actual bug or a red herring from Siteimprove.
  2. If bug, write a patch with tests.
  3. If red herring, write to Siteimprove to fix their tests.

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3096790

Command icon 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:

Comments

thatguy created an issue. See original summary.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

glugmeister’s picture

The 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).

mrshowerman’s picture

Status: Active » Needs review
StatusFileSize
new2.67 KB

Here'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.

mrshowerman’s picture

StatusFileSize
new2.56 KB

Changed states.js manually by mistake in #6

duaelfr’s picture

I 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.

jwilson3’s picture

Issue summary: View changes
Issue tags: +Needs accessibility review, +aria, +html5

I'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.

jwilson3’s picture

Issue summary: View changes
SomeIntern’s picture

Version: 9.3.x-dev » 9.4.x-dev
Status: Needs review » Reviewed & tested by the community

Manually tested with 9.4 and can confirm the patch applied and worked. Setting to RTBC.

SomeIntern’s picture

Version: 9.4.x-dev » 9.3.x-dev

Reverting metadata changed by mistake.

damienmckenna’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

It seems like this was duplicated in #3213473, which also has test coverage. Maybe the two should be combined?

mrshowerman’s picture

#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.

swirt’s picture

I agree that #7 would be a better solution than what is being done in https://www.drupal.org/project/drupal/issues/3213473

netboss’s picture

Just applied and tested #7 and it's working fine for us. And I also agree with swirt, this patch should be used instead.

swirt’s picture

Status: Needs work » Reviewed & tested by the community

Just 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

danflanagan8’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new6.74 KB

That 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::setAttributes function. 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 the setAttributes function somehow.

Status: Needs review » Needs work

The last submitted patch, 18: 3096790-18-FAIL.patch, failed testing. View results

danflanagan8’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new9.3 KB
new2.56 KB

Those 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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mohammed motar’s picture

The patch in #20 works well in our environment

mohammed motar’s picture

The patch in #20 works well in our environment.

Manoj Raj.R’s picture

#20 looks good to me.
+1 RTBC

gaurav-mathur’s picture

Patch #20 applied successfully on drupal version 9 and working fine.
+1 RTBC

mgifford’s picture

This looks like a good change. We should default to HTML & not have redundant ARIA.

jwilson3’s picture

Title: Remove aria-required attribute if unneeded » aria-required attribute is redundant when required attribute is present
mrshowerman’s picture

StatusFileSize
new9.3 KB
new1.78 KB
new8.31 KB

Re-rolling for 9.5.x and 10.1.x.

mgifford’s picture

Issue tags: +wcag412

Tagged for 4.1.2.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

This 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

1) Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest::testJavascriptStates
Failed asserting that true is false.

Changes look good to me.

xjm’s picture

Thanks 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.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs accessibility review
  1. +++ b/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php
    @@ -109,4 +109,106 @@ public function testPreRenderAjaxFormWithQueryOptions() {
    +  /**
    +   * @covers ::setAttributes
    +   *
    +   * @dataProvider providerTestSetAttributes
    +   */
    +  public function testSetAttributes($element, $class, $expected) {
    +    RenderElement::setAttributes($element, $class);
    +    $this->assertSame($expected, $element);
    +  }
    

    This should have parameter documentation for the parameters from the data provider, and parameter and return typehints.

  2. +++ b/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php
    @@ -109,4 +109,106 @@ public function testPreRenderAjaxFormWithQueryOptions() {
    +   * Provides test data for testSetAttributes().
    +   */
    +  public function providerTestSetAttributes() {
    

    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:

    Array of test data, keyed by a short description of the scenario under test. See testSetAttributes() for documentation of the test parameters.

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 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).

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.

xjm credited catch.

xjm credited EthanT.

xjm credited guilhermevp.

xjm credited larowlan.

xjm credited paulocs.

xjm’s picture

I closed #3213473: Checkbox input can't have aria-required attribute as a duplicate and am adding credits from that issue here.

xjm credited henry.odiete.

xjm’s picture

 

mgifford’s picture

@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.

mrshowerman’s picture

StatusFileSize
new8.37 KB

Re-roll against current 10.1.x. Leaving in NW as per #34.

bnjmnm’s picture

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.

It 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mohammed motar’s picture

StatusFileSize
new8.63 KB

I have created a patch file from the latest commit in the merge request.

patricia_zoocha’s picture

Patch #49 applied successfully on Drupal version 10.3.6 and working fine.

mgifford’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

left some comments on the MR.

dcam made their first commit to this issue’s fork.

dcam’s picture

Status: Needs work » Needs review

Feedback was addressed or responded to.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Been 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.

  • nod_ committed bf98f857 on 11.x
    Issue #3096790 by mrshowerman, danflanagan8, dcam, smustgrave, mgifford...
nod_’s picture

Status: Reviewed & tested by the community » Fixed

Committed bf98f85 and pushed to 11.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

liam morland’s picture

Will this be backported to Drupal 10? There is old discussion above about backporting but no decision.