diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php index 9c5167b80..5b826e8d5 100644 --- a/core/lib/Drupal/Core/Render/Element/RenderElement.php +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php @@ -140,7 +140,9 @@ public static function setAttributes(&$element, $class = []) { $element['#attributes']['class'][] = 'required'; $element['#attributes']['required'] = 'required'; // Fieldset elements are not allowed to have aria-required attribute. - if ($element['#type'] !== 'fieldset') { + // Checkbox elements are not allowed to have aria-required attribute. + // See https://www.w3.org/TR/wai-aria-1.2/#checkbox. + if ($element['#type'] !== 'fieldset' && $element['#type'] !== 'checkbox') { $element['#attributes']['aria-required'] = 'true'; } } diff --git a/core/modules/system/tests/src/Functional/Form/CheckboxTest.php b/core/modules/system/tests/src/Functional/Form/CheckboxTest.php index c28112383..7b5a06372 100644 --- a/core/modules/system/tests/src/Functional/Form/CheckboxTest.php +++ b/core/modules/system/tests/src/Functional/Form/CheckboxTest.php @@ -102,6 +102,10 @@ public function testFormCheckbox() { $name = (string) $checkbox->getAttribute('name'); $this->assertSame($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', new FormattableMarkup('Checkbox %name correctly checked', ['%name' => $name])); } + + // Ensure that checkbox has no aria required attribute. + $this->drupalGet('/form-test/checkbox'); + $this->assertEmpty($this->assertSession()->elementExists('css', 'input[type=checkbox][required]')->getAttribute('aria-required')); } }