Browse Source

Fix #18967: Use proper attribute names for tabular data in `yii\widgets\ActiveField::addAriaAttributes()`

tags/2.0.44
AnkIF 3 years ago committed by GitHub
parent
commit
6aa5bd8d46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 7
      framework/widgets/ActiveField.php
  3. 56
      tests/framework/widgets/ActiveFieldTest.php

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

7
framework/widgets/ActiveField.php

@ -927,11 +927,14 @@ class ActiveField extends Component
*/
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';
}
}

56
tests/framework/widgets/ActiveFieldTest.php

@ -530,6 +530,62 @@ EOD;
$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];

Loading…
Cancel
Save