Skip to content

Commit

Permalink
Add checkedValue attribute to AbstractChoiceList. (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 6, 2024
1 parent 63965bb commit cd96458
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/Input/Base/AbstractChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract class AbstractChoiceList extends Element implements ChoiceInterface
use Attribute\Aria\HasAriaLabel;
use Attribute\CanBeAutofocus;
use Attribute\Custom\HasAttributes;
use Attribute\Custom\HasCheckedValue;
use Attribute\Custom\HasContainer;
use Attribute\Custom\HasEnclosedByLabel;
use Attribute\Custom\HasLabel;
Expand Down Expand Up @@ -57,9 +58,7 @@ public function items(Checkbox|Radio ...$items): static

protected function run(): string
{
$value = $this->getValue();

$this->validateScalar($value);
$this->validateScalar($this->checkedValue);

$attributes = $this->attributes;
$containerAttributes = $this->containerAttributes;
Expand Down Expand Up @@ -89,7 +88,7 @@ protected function run(): string
foreach ($items as $item) {
$listItem = $item
->attributes($attributes)
->checked($value === $item->getValue())
->checked($this->checkedValue === $item->getValue())
->enclosedByLabel($this->enclosedByLabel)
->id(null)
->separator($this->separator);
Expand Down
24 changes: 24 additions & 0 deletions tests/Input/ChoiceList/CheckboxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,29 @@ public function testTabindex(): void
}

public function testValue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="choice-list-65858c272ea89">
<input name="CheckboxForm[text]" type="checkbox" value="1" checked>
<label>Female</label>
<input name="CheckboxForm[text]" type="checkbox" value="2">
<label>Male</label>
</div>
HTML,
Choicelist::widget()
->checkedValue(1)
->id('choice-list-65858c272ea89')
->items(
Checkbox::widget()->labelContent('Female')->value(1),
Checkbox::widget()->labelContent('Male')->value(2),
)
->name('CheckboxForm[text]')
->render(),
);
}

public function testValueWithNull(): void
{
Assert::equalsWithoutLE(
<<<HTML
Expand All @@ -463,6 +486,7 @@ public function testValue(): void
</div>
HTML,
Choicelist::widget()
->checkedValue(null)
->id('choice-list-65858c272ea89')
->items(
Checkbox::widget()->labelContent('Female')->value(1),
Expand Down
2 changes: 1 addition & 1 deletion tests/Input/ChoiceList/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function testValue(): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('ChoiceList::class widget must be a scalar value.');

ChoiceList::widget()->items(Checkbox::widget())->value([])->render();
ChoiceList::widget()->items(Checkbox::widget())->checkedValue([])->render();
}
}
24 changes: 24 additions & 0 deletions tests/Input/ChoiceList/RadioListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,29 @@ public function testTabindex(): void
}

public function testValue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="choice-list-65858c272ea89">
<input name="radioform[text]" type="radio" value="1" checked>
<label>Female</label>
<input name="radioform[text]" type="radio" value="2">
<label>Male</label>
</div>
HTML,
ChoiceList::widget()
->checkedValue(1)
->id('choice-list-65858c272ea89')
->items(
Radio::widget()->labelContent('Female')->value(1),
Radio::widget()->labelContent('Male')->value(2),
)
->name('radioform[text]')
->render(),
);
}

public function testValueWithNull(): void
{
Assert::equalsWithoutLE(
<<<HTML
Expand All @@ -395,6 +418,7 @@ public function testValue(): void
</div>
HTML,
ChoiceList::widget()
->checkedValue(null)
->id('choice-list-65858c272ea89')
->items(
Radio::widget()->labelContent('Female')->value(1),
Expand Down

0 comments on commit cd96458

Please sign in to comment.