Skip to content

Commit 050af98

Browse files
AndersonFriacatshafer
authored andcommitted
Possibility of boolean value for selected option in select field (LaravelCollective#487)
* Validation in select field for boolean value of selected option * Adjusts * Update FormBuilder.php Adjustments * Test and adjusment on PHPDoc
1 parent 360cb18 commit 050af98

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/FormBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ protected function setQuickTextAreaSize($options)
572572
*
573573
* @param string $name
574574
* @param array $list
575-
* @param string $selected
575+
* @param string|bool $selected
576576
* @param array $selectAttributes
577577
* @param array $optionsAttributes
578578
* @param array $optgroupsAttributes
@@ -787,7 +787,9 @@ protected function getSelectedValue($value, $selected)
787787
} elseif ($selected instanceof Collection) {
788788
return $selected->contains($value) ? 'selected' : null;
789789
}
790-
790+
if (is_int($value) && is_bool($selected)) {
791+
return (bool)$value === $selected;
792+
}
791793
return ((string) $value === (string) $selected) ? 'selected' : null;
792794
}
793795

tests/FormBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ public function testSelect()
464464
'<select name="countries"><option value="1" selected="selected">L</option><option value="2">M</option></select>',
465465
$result
466466
);
467+
468+
$select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']);
469+
$this->assertEquals(
470+
'<select name="avc"><option value="" hidden="hidden">Select</option><option value="1" selected>Yes</option><option value="0" >No</option></select>',
471+
$select
472+
);
467473
}
468474

469475
public function testSelectCollection()

0 commit comments

Comments
 (0)