Skip to content

Commit

Permalink
Fix yiisoft#18967: Use proper attribute names for tabular data in `yi…
Browse files Browse the repository at this point in the history
…i\widgets\ActiveField::addAriaAttributes()`
  • Loading branch information
AnkIF authored Nov 22, 2021
1 parent 7d789bd commit 6aa5bd8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions framework/widgets/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
Expand Down
56 changes: 56 additions & 0 deletions tests/framework/widgets/ActiveFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<div class="form-group field-activefieldtestmodel-0-attributename">
<label class="control-label" for="activefieldtestmodel-0-attributename">Attribute Name</label>
<input type="text" id="activefieldtestmodel-0-attributename" class="form-control" name="ActiveFieldTestModel[0][attributeName]">
<div class="hint-block">Hint for attributeName attribute</div>
<div class="help-block"></div>
</div>
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'
<div class="form-group field-activefieldtestmodel-0-attributename required">
<label class="control-label" for="activefieldtestmodel-0-attributename">Attribute Name</label>
<input type="text" id="activefieldtestmodel-0-attributename" class="form-control" name="ActiveFieldTestModel[0][attributeName]" aria-required="true">
<div class="hint-block">Hint for attributeName attribute</div>
<div class="help-block"></div>
</div>
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'
<div class="form-group field-activefieldtestmodel-0-attributename has-error">
<label class="control-label" for="activefieldtestmodel-0-attributename">Attribute Name</label>
<input type="text" id="activefieldtestmodel-0-attributename" class="form-control" name="ActiveFieldTestModel[0][attributeName]" aria-invalid="true">
<div class="hint-block">Hint for attributeName attribute</div>
<div class="help-block">Some error</div>
</div>
EOD;

$actualValue = $this->activeField->render();
$this->assertEqualsWithoutLE($expectedValue, $actualValue);
}

public function testEmptyTag()
{
$this->activeField->options = ['tag' => false];
Expand Down

0 comments on commit 6aa5bd8

Please sign in to comment.