Skip to content

Commit

Permalink
Add support null value. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Nov 2, 2021
1 parent a191c04 commit e7aac20
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
9 changes: 6 additions & 3 deletions src/Widget/CheckboxList.php
Expand Up @@ -218,8 +218,8 @@ protected function run(): string
/** @var iterable<int, scalar|Stringable>|scalar|Stringable|null */
$value = HtmlForm::getAttributeValue($new->getFormModel(), $new->attribute);

if (!is_iterable($value)) {
throw new InvalidArgumentException('CheckboxList widget value must always be an array.');
if (!is_iterable($value) && null !== $value) {
throw new InvalidArgumentException('CheckboxList widget must be a array or null value.');
}

/** @var string */
Expand All @@ -241,14 +241,17 @@ protected function run(): string
$checkboxList = $checkboxList->separator($new->separator);
}

if ($value !== null) {
$checkboxList = $checkboxList->values($value);
}

return $checkboxList
->checkboxAttributes($new->attributes)
->containerAttributes($new->containerAttributes)
->containerTag($new->containerTag)
->individualInputAttributes($new->individualItemsAttributes)
->itemFormatter($new->itemsFormatter)
->replaceCheckboxAttributes($new->itemsAttributes)
->values($value)
->render();
}
}
2 changes: 1 addition & 1 deletion tests/TestSupport/Form/TypeForm.php
Expand Up @@ -8,7 +8,7 @@

final class TypeForm extends FormModel
{
private array $array = [];
private ?array $array = [];
private bool $bool = false;
private float $float = 0;
private int $int = 0;
Expand Down
42 changes: 23 additions & 19 deletions tests/Widget/CheckboxListTest.php
Expand Up @@ -23,11 +23,10 @@ final class CheckboxListTest extends TestCase

public function testContainerAttributes(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div id="typeform-array" class="test-class">
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
$html = CheckboxList::widget()
Expand All @@ -40,10 +39,9 @@ public function testContainerAttributes(): void

public function testContainerTag(): void
{
$this->formModel->setAttribute('array', [1]);
$expected = <<<'HTML'
<span id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" checked> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</span>
HTML;
Expand All @@ -57,9 +55,8 @@ public function testContainerTag(): void

public function testContainerTagWithNull(): void
{
$this->formModel->setAttribute('array', [1]);
$expected = <<<'HTML'
<label><input type="checkbox" name="TypeForm[array][]" value="1" checked> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
HTML;
$html = CheckboxList::widget()
Expand All @@ -84,11 +81,10 @@ public function testDisabled(): void

public function testIndividualItemsAttributes(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" disabled> Female</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
$html = CheckboxList::widget()
Expand All @@ -101,11 +97,10 @@ public function testIndividualItemsAttributes(): void

public function testItemsAttributes(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
$html = CheckboxList::widget()
Expand All @@ -118,11 +113,10 @@ public function testItemsAttributes(): void

public function testItemFormater(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type='checkbox' name='TypeForm[array][]' value='1'> Female</label>
<label><input type='checkbox' name='TypeForm[array][]' value='2' checked> Male</label>
<label><input type='checkbox' name='TypeForm[array][]' value='2'> Male</label>
</div>
HTML;
$html = CheckboxList::widget()
Expand Down Expand Up @@ -171,11 +165,10 @@ public function testItemsFromValues(): void

public function testReadOnly(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" readonly> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2" checked readonly> Male</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2" readonly> Male</label>
</div>
HTML;
$this->assertEqualsWithoutLE(
Expand All @@ -186,10 +179,9 @@ public function testReadOnly(): void

public function testRender(): void
{
$this->formModel->setAttribute('array', [1]);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" checked> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
Expand All @@ -201,10 +193,9 @@ public function testRender(): void

public function testSeparator(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>&#9866;<label><input type="checkbox" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>&#9866;<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
$html = CheckboxList::widget()
Expand All @@ -217,6 +208,19 @@ public function testSeparator(): void

public function testValue(): void
{
// value null
$this->formModel->setAttribute('array', null);
$expected = <<<'HTML'
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
$this->assertEqualsWithoutLE(
$expected,
CheckboxList::widget()->config($this->formModel, 'array')->items($this->sex)->render(),
);

// value iterable
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
Expand All @@ -235,7 +239,7 @@ public function testValueException(): void
{
$this->formModel->setAttribute('int', 1);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('CheckboxList widget value must always be an array');
$this->expectExceptionMessage('CheckboxList widget must be a array or null value.');
CheckboxList::widget()->config($this->formModel, 'int')->render();
}

Expand Down
45 changes: 26 additions & 19 deletions tests/Widget/FieldCheckBoxListTest.php
Expand Up @@ -24,13 +24,12 @@ final class FieldCheckBoxListTest extends TestCase

public function testContainerAttributes(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array" class="test-class">
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
</div>
HTML;
Expand All @@ -43,12 +42,11 @@ public function testContainerAttributes(): void

public function testContainerTag(): void
{
$this->formModel->setAttribute('array', [1]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<span id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" checked> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</span>
</div>
Expand All @@ -62,11 +60,10 @@ public function testContainerTag(): void

public function testContainerTagWithNull(): void
{
$this->formModel->setAttribute('array', [1]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1" checked> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
HTML;
Expand Down Expand Up @@ -97,13 +94,12 @@ public function testDisabled(): void

public function testIndividualItemsAttributes(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" disabled> Female</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2"> Male</label>
</div>
</div>
HTML;
Expand All @@ -121,13 +117,12 @@ public function testIndividualItemsAttributes(): void

public function testItemsAttributes(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" class="test-class" name="TypeForm[array][]" value="2"> Male</label>
</div>
</div>
HTML;
Expand All @@ -140,13 +135,12 @@ public function testItemsAttributes(): void

public function testItemFormater(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type='checkbox' name='TypeForm[array][]' value='1'> Female</label>
<label><input type='checkbox' name='TypeForm[array][]' value='2' checked> Male</label>
<label><input type='checkbox' name='TypeForm[array][]' value='2'> Male</label>
</div>
</div>
HTML;
Expand Down Expand Up @@ -187,13 +181,12 @@ public function testItemsFromValues(): void

public function testReadOnly(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" readonly> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2" checked readonly> Male</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2" readonly> Male</label>
</div>
</div>
HTML;
Expand All @@ -208,12 +201,11 @@ public function testReadOnly(): void

public function testRender(): void
{
$this->formModel->setAttribute('array', [1]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1" checked> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
</div>
Expand All @@ -226,12 +218,11 @@ public function testRender(): void

public function testSeparator(): void
{
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>&#9866;<label><input type="checkbox" name="TypeForm[array][]" value="2" checked> Male</label>
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>&#9866;<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
</div>
HTML;
Expand All @@ -244,6 +235,22 @@ public function testSeparator(): void

public function testValue(): void
{
// value null
$this->formModel->setAttribute('array', null);
$expected = <<<'HTML'
<div>
<label for="typeform-array">Array</label>
<div id="typeform-array">
<label><input type="checkbox" name="TypeForm[array][]" value="1"> Female</label>
<label><input type="checkbox" name="TypeForm[array][]" value="2"> Male</label>
</div>
</div>
HTML;
$this->assertEqualsWithoutLE(
$expected,
Field::widget()->config($this->formModel, 'array')->checkboxList([], $this->sex)->render(),
);

// value iterable
$this->formModel->setAttribute('array', [2]);
$expected = <<<'HTML'
Expand All @@ -265,7 +272,7 @@ public function testValueException(): void
{
$this->formModel->setAttribute('object', new StdClass());
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('CheckboxList widget value must always be an array');
$this->expectExceptionMessage('CheckboxList widget must be a array or null value.');
Field::widget()->config($this->formModel, 'object')->checkboxList()->render();
}

Expand Down

0 comments on commit e7aac20

Please sign in to comment.