From 6aa5bd8d4649970cf681aa9d92b6264fa8aad012 Mon Sep 17 00:00:00 2001 From: AnkIF <33079906+AnkIF@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:00:45 +0700 Subject: [PATCH] Fix #18967: Use proper attribute names for tabular data in `yii\widgets\ActiveField::addAriaAttributes()` --- framework/CHANGELOG.md | 1 + framework/widgets/ActiveField.php | 7 ++- tests/framework/widgets/ActiveFieldTest.php | 56 +++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index a10bc6b6774..48ab98f2aac 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.44 under development ------------------------ +- Enh #18967: Use proper attribute names for tabular data in `yii\widgets\ActiveField::addAriaAttributes()` (AnkIF) - Bug #18798: Fix `StringHelper::dirname()` when passing string with a trailing slash (perlexed) - Enh #18328: Raise warning when trying to register a file after `View::endPage()` has been called (perlexed) - Enh #18812: Added error messages and optimized "error" methods in `yii\helpers\BaseJson` (WinterSilence, samdark) diff --git a/framework/widgets/ActiveField.php b/framework/widgets/ActiveField.php index c49394ddd98..9d86aeebab0 100644 --- a/framework/widgets/ActiveField.php +++ b/framework/widgets/ActiveField.php @@ -927,11 +927,14 @@ protected function getInputId() */ protected function addAriaAttributes(&$options) { + // Get proper attribute name when attribute name is tabular. + $attributeName = Html::getAttributeName($this->attribute); + if ($this->addAriaAttributes) { - if (!isset($options['aria-required']) && $this->model->isAttributeRequired($this->attribute)) { + if (!isset($options['aria-required']) && $this->model->isAttributeRequired($attributeName)) { $options['aria-required'] = 'true'; } - if (!isset($options['aria-invalid']) && $this->model->hasErrors($this->attribute)) { + if (!isset($options['aria-invalid']) && $this->model->hasErrors($attributeName)) { $options['aria-invalid'] = 'true'; } } diff --git a/tests/framework/widgets/ActiveFieldTest.php b/tests/framework/widgets/ActiveFieldTest.php index ecb4cdc6b2d..201ca38578c 100644 --- a/tests/framework/widgets/ActiveFieldTest.php +++ b/tests/framework/widgets/ActiveFieldTest.php @@ -530,6 +530,62 @@ public function testAriaInvalidAttribute() $this->assertEqualsWithoutLE($expectedValue, $actualValue); } + public function testTabularAriaAttributes() + { + $this->activeField->attribute = '[0]' . $this->attributeName; + $this->activeField->addAriaAttributes = true; + + $expectedValue = <<<'EOD' +
+ + +
Hint for attributeName attribute
+
+
+EOD; + + $actualValue = $this->activeField->render(); + $this->assertEqualsWithoutLE($expectedValue, $actualValue); + } + + public function testTabularAriaRequiredAttribute() + { + $this->activeField->attribute = '[0]' . $this->attributeName; + $this->activeField->addAriaAttributes = true; + $this->helperModel->addRule([$this->attributeName], 'required'); + + $expectedValue = <<<'EOD' +
+ + +
Hint for attributeName attribute
+
+
+EOD; + + $actualValue = $this->activeField->render(); + $this->assertEqualsWithoutLE($expectedValue, $actualValue); + } + + public function testTabularAriaInvalidAttribute() + { + $this->activeField->attribute = '[0]' . $this->attributeName; + $this->activeField->addAriaAttributes = true; + $this->helperModel->addError($this->attributeName, 'Some error'); + + $expectedValue = <<<'EOD' +
+ + +
Hint for attributeName attribute
+
Some error
+
+EOD; + + $actualValue = $this->activeField->render(); + $this->assertEqualsWithoutLE($expectedValue, $actualValue); + } + public function testEmptyTag() { $this->activeField->options = ['tag' => false];