Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix show error for checkbox, radio, checkboxList, radioList #208

Merged
merged 4 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Yii Framework 2 bootstrap4 extension Change Log
------------------------

- Enh #205: Added `Toast` widget (simialbi)

- Bug #173: Fix bug with error messages not being displayed for checkbox, radio, checkboxList, and radioList (egrekov)

2.0.9 November 10, 2020
-----------------------
Expand Down
4 changes: 4 additions & 0 deletions src/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public function checkbox($options = [], $enclosedByLabel = false)
}
}

$this->addErrorClassIfNeeded($options);
return parent::checkbox($options, false);
}

Expand Down Expand Up @@ -313,6 +314,7 @@ public function radio($options = [], $enclosedByLabel = false)
$this->parts['{labelTitle}'] = $options['label'];
}

$this->addErrorClassIfNeeded($options);
return parent::radio($options, false);
}

Expand Down Expand Up @@ -342,6 +344,7 @@ public function checkboxList($items, $options = [])
Html::addCssClass($wrapperOptions, 'custom-control-inline');
}

$this->addErrorClassIfNeeded($options);
samdark marked this conversation as resolved.
Show resolved Hide resolved
$html = Html::beginTag('div', $wrapperOptions) . "\n" .
Html::checkbox($name, $checked, $options) . "\n";
if ($itemCount === $i) {
Expand Down Expand Up @@ -383,6 +386,7 @@ public function radioList($items, $options = [])
Html::addCssClass($wrapperOptions, 'custom-control-inline');
}

$this->addErrorClassIfNeeded($options);
samdark marked this conversation as resolved.
Show resolved Hide resolved
$html = Html::beginTag('div', $wrapperOptions) . "\n" .
Html::radio($name, $checked, $options) . "\n";
if ($itemCount === $i) {
Expand Down
90 changes: 90 additions & 0 deletions tests/ActiveFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,51 @@ public function testRadioList()
</div>
</div>

</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);
}

public function testRadioError()
{
Html::$counter = 0;
$this->helperModel->addError($this->attributeName, 'Test print error message');
$html = $this->activeField->radio()->render();

$expectedHtml = <<<HTML
<div class="form-group field-dynamicmodel-attributename">
<div class="custom-control custom-radio">
<input type="hidden" name="DynamicModel[attributeName]" value="0"><input type="radio" id="dynamicmodel-attributename" class="custom-control-input is-invalid" name="DynamicModel[attributeName]" value="1" aria-invalid="true">
<label class="custom-control-label" for="dynamicmodel-attributename">Attribute Name</label>
<div class="invalid-feedback">Test print error message</div>

</div>
</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);
}

public function testRadioListError()
{
Html::$counter = 0;
$this->helperModel->addError($this->attributeName, 'Test print error message');
$html = $this->activeField->radioList([1 => 'name1', 2 => 'name2'])->render();

$expectedHtml = <<<HTML
<div class="form-group field-dynamicmodel-attributename">
<label>Attribute Name</label>
<input type="hidden" name="DynamicModel[attributeName]" value=""><div id="dynamicmodel-attributename" class="is-invalid" role="radiogroup" aria-invalid="true"><div class="custom-control custom-radio">
<input type="radio" id="i0" class="custom-control-input is-invalid" name="DynamicModel[attributeName]" value="1">
<label class="custom-control-label" for="i0">name1</label>
</div>

<div class="custom-control custom-radio">
<input type="radio" id="i1" class="custom-control-input is-invalid" name="DynamicModel[attributeName]" value="2">
<label class="custom-control-label" for="i1">name2</label>
<div class="invalid-feedback">Test print error message</div>
</div>
</div>

</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);
Expand All @@ -109,6 +154,51 @@ public function testCheckboxList()
</div>
</div>

</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);
}

public function testCheckboxError()
{
Html::$counter = 0;
$this->helperModel->addError($this->attributeName, 'Test print error message');
$html = $this->activeField->checkbox()->render();

$expectedHtml = <<<HTML
<div class="form-group field-dynamicmodel-attributename">
<div class="custom-control custom-checkbox">
<input type="hidden" name="DynamicModel[attributeName]" value="0"><input type="checkbox" id="dynamicmodel-attributename" class="custom-control-input is-invalid" name="DynamicModel[attributeName]" value="1" aria-invalid="true">
<label class="custom-control-label" for="dynamicmodel-attributename">Attribute Name</label>
<div class="invalid-feedback">Test print error message</div>

</div>
</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);
}

public function testCheckboxListError()
{
Html::$counter = 0;
$this->helperModel->addError($this->attributeName, 'Test print error message');
$html = $this->activeField->checkboxList([1 => 'name1', 2 => 'name2'])->render();

$expectedHtml = <<<HTML
<div class="form-group field-dynamicmodel-attributename">
<label>Attribute Name</label>
<input type="hidden" name="DynamicModel[attributeName]" value=""><div id="dynamicmodel-attributename" class="is-invalid" aria-invalid="true"><div class="custom-control custom-checkbox">
<input type="checkbox" id="i0" class="custom-control-input is-invalid" name="DynamicModel[attributeName][]" value="1">
<label class="custom-control-label" for="i0">name1</label>
</div>

<div class="custom-control custom-checkbox">
<input type="checkbox" id="i1" class="custom-control-input is-invalid" name="DynamicModel[attributeName][]" value="2">
<label class="custom-control-label" for="i1">name2</label>
<div class="invalid-feedback">Test print error message</div>
</div>
</div>

</div>
HTML;
$this->assertEqualsWithoutLE($expectedHtml, $html);
Expand Down