Problem/Motivation

ARIA 1.1 specifies that the aria-required attribute isn't allowed on an element with role="checkbox"--including an input with a checkbox type. aria-required is allowed on checkboxes role. This was confirmed by Wilco Fiers that aria-required wasn't intended to be allowed on the checkbox role.

Steps to reproduce

1) Add a boolean field to the Article content type.
2) Set the field as required.
3) Inspect the checkbox field in the form page to add a new article and see that the aria-required attribute is set.

Comments

paulocs created an issue. See original summary.

paulocs’s picture

Status: Active » Needs review
StatusFileSize
new854 bytes
paulocs’s picture

Title: Checkbox input shouldn't have aria-required attribute » Checkbox input can't have aria-required attribute

Status: Needs review » Needs work

The last submitted patch, 2: 3213473-2.patch, failed testing. View results

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new966 bytes
paulocs’s picture

Please not consider patch #5. I'll attach a new one.

paulocs’s picture

StatusFileSize
new996 bytes
paulocs’s picture

Issue tags: +Accessibility
guilhermevp’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new59.59 KB
new46.85 KB

Patch works as intended.

Before:

1

After:

2

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Bug Smash Initiative, +Needs tests
+++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
@@ -139,7 +139,12 @@ public static function setAttributes(&$element, $class = []) {
+      if (isset($element['#type']) && $element['#type'] != 'checkbox') {
+        $element['#attributes']['aria-required'] = 'true';
+      }
+      elseif (!isset($element['#type'])) {
+        $element['#attributes']['aria-required'] = 'true';
+      }

we can do this in one line

if (($element['#type'] ?? NULL) !== 'checkbox') {
  // ...
}

As this is a bug-fix, we need test-coverage that confirms it is fixed to ensure it stays fixed. I guess \Drupal\Tests\system\Functional\Form\CheckboxTest would be a good place to look to add it.

gauravvvv’s picture

StatusFileSize
new865 bytes
new838 bytes

Re-rolled patch #7, Attached interdiff for same. Please review.

gauravvvv’s picture

Status: Needs work » Needs review
paulocs’s picture

Status: Needs review » Needs work

Needs tests.

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new668 bytes
new1.5 KB

Adding test only patch and full patch.

The last submitted patch, 14: 3213473-TEST-ONLY.patch, failed testing. View results

marcusvsouza’s picture

Status: Needs review » Reviewed & tested by the community

RTBC!

paulocs’s picture

Issue tags: -Needs tests
larowlan’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/system/tests/src/Functional/Form/CheckboxTest.php
@@ -102,4 +102,13 @@ public function testFormCheckbox() {
+    $element = $this->cssSelect('input[aria-required]');

can we add a positive assert here to.

E.g. assert that there is a single checkbox on the page. otherwise someone could remove the checkbox from that form and this test would happily keep passing, but be redundant

henry.odiete’s picture

Status: Needs work » Needs review
StatusFileSize
new1.6 KB

Made modifications to the test to add coverage for if checkbox exist

henry.odiete’s picture

StatusFileSize
new1.6 KB
chetanbharambe’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new569.31 KB
new519.98 KB

Verified and tested patch #20.
Patch applied successfully and looks good to me.

Testing Steps:
# Goto: admin/structure/types/manage/article/fields
# Click on Add field
# Select a Boolean from "Add a new field"
# Mention the Label name
# Set the field as required.
# Click on save
# Go: node/add/article
# User will able to see checkbox of Boolean

Expected Results:
# Inspect the checkbox field in the form page to add a new article and see that the required attribute is set

Actual Results:
# Inspect the checkbox field in the form page to add a new article and see that the aria-required attribute is set.

Looks good to me.
Can be a move to RTBC.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/system/tests/src/Functional/Form/CheckboxTest.php
@@ -102,4 +102,15 @@ public function testFormCheckbox() {
+    $checkbox = $this->cssSelect('input[type=checkbox]');
+    $this->assertNotEmpty($checkbox);

we could use

$this->assertEmpty($this->assertSession()->elementExists('css', 'input[type=checkbox][required]')->getAttribute('aria-required'));

here and save some lines, but also ensure that we are adding a positive assert that the checkbox is required. At present, someone could remove the #required attribute from that checkbox and this test would pass.

Also, are there any existing test methods we could use here.

When running a functional test, each new test{Something} adds the setup cost of a new install

paulocs’s picture

Working on it

paulocs’s picture

StatusFileSize
new1.15 KB
new1.74 KB

Thanks for the feedback @larowlan. Makes totally sense.
I moved the test to testFormCheckbox() and rewrite it based on comment #22.

guilhermevp’s picture

Status: Needs review » Reviewed & tested by the community

Moving to RTBC, after updated changes.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 24: 3213473-24.patch, failed testing. View results

meenakshi_j’s picture

Status: Needs work » Needs review
StatusFileSize
new6.97 KB
new6.19 KB

Fixed the #24 fails.

paulocs’s picture

StatusFileSize
new1.74 KB

Hi @Meenakshi_j,

why did you re-add a new function to test it?
Comment #22 suggests to add it in another function to avoid waste of time installing a new drupal and patch #24 addresses it.
There is also a CheckboxTest.php.orig in the patch. It shouldn't be there.

I'm re-adding patch #24. I suspect this is a random fail.

paulocs’s picture

Status: Needs review » Reviewed & tested by the community

Setting back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
@@ -139,7 +139,9 @@ public static function setAttributes(&$element, $class = []) {
       $element['#attributes']['required'] = 'required';
-      $element['#attributes']['aria-required'] = 'true';
+      if (($element['#type'] ?? NULL) !== 'checkbox') {
+        $element['#attributes']['aria-required'] = 'true';
+      }
     }

Let's add an inline link to the W3C documentation.

I'm also slightly confused that this isn't allowed, since the 'required checkbox' is a common element on a lot of forms these days (usually for terms and conditions).

ethant’s picture

StatusFileSize
new1.94 KB

Had to reroll patch to apply cleanly to 9.27
Incorrect pathing - updated patch in next comment.

ethant’s picture

StatusFileSize
new1.78 KB
ethant’s picture

StatusFileSize
new1.98 KB

Edit: added patch 33, but realized approach is incorrect. 32 works.

ethant’s picture

swirt’s picture

Status: Needs work » Reviewed & tested by the community

#32 tested and works as desired. Also accounts for request in #30.

I also agree with Catch. It seems odd to not allow the aria role of required on a checkbox as it is a common pattern. I think the issue is that there was no agreement on how the role should behave for multiple checkboxes. And if there is no agreement on the role, it could not be spec'd as how it should behave, so it could not be included as a possible role.

ethant’s picture

StatusFileSize
new1.77 KB

Another version of patch to remove unnecessary NULL check on checkbox.

paulocs’s picture

Status: Reviewed & tested by the community » Needs work
swirt’s picture

#36 works. Unclear what the request is for the change back to "needs work".

ethant’s picture

StatusFileSize
new1.81 KB

Forgot to account for "checkboxes." This patch adds.

ethant’s picture

Status: Needs work » Needs review
damienmckenna’s picture

Status: Needs review » Needs work

Having reviewed the latest patch it appears to need to be rerolled, and per the coding standards the "See" line should be written in the format "@see [URL]".

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.91 KB

Rerolled, and updated the syntax of the "@see" comment.

SomeIntern’s picture

Status: Needs review » Reviewed & tested by the community

Manually tested the output and can confirm it worked against 9.4. I'm setting this back to RTBC because the patch now applies cleanly.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 42: drupal-n3213473-41.patch, failed testing. View results

damienmckenna’s picture

ethant’s picture

Good q, @damienmckenna. It seems to me that https://www.drupal.org/project/drupal/issues/3096790 is recommending that we pull out aria-required, entirely. I think I could get behind this initiative. Per https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Tec...,

Note: HTML also specifies the required attribute which is supported well in user agents. Use aria-required for backwards compatibility only.
vikashsoni’s picture

StatusFileSize
new12.4 KB
new30.52 KB

Applied patch #18 applied successfully and looks good for me
for ref sharing screenshot.....

swirt’s picture

Just to note DamienMcKenna's 42 altered EthanT's 39 patch slightly.
#39 excluded checkbox, checkboxes, and fieldset
#42 excluded checkbox, and checkboxes

fieldsets is another one that does not support aria-required. https://www.drupal.org/project/drupal/issues/3205691
Unfortunately both of these issues and related patches touch the same lines and pattern which makes them messy to apply.

I suggest changing from a simple string matching to an in_array pattern where things can be added without calamity. Patch coming.

swirt’s picture

Suggesting that we close this issue and put our support behind https://www.drupal.org/project/drupal/issues/3096790

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.

xjm’s picture

Status: Needs work » Closed (duplicate)