Skip to content

Commit

Permalink
[5.6] Validation in select field for boolean value of selected option (
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonFriaca authored and tshafer committed Feb 12, 2018
1 parent 818efc2 commit 41cd929
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ protected function setQuickTextAreaSize($options)
*
* @param string $name
* @param array $list
* @param string $selected
* @param string|bool $selected
* @param array $selectAttributes
* @param array $optionsAttributes
* @param array $optgroupsAttributes
Expand Down Expand Up @@ -787,7 +787,9 @@ protected function getSelectedValue($value, $selected)
} elseif ($selected instanceof Collection) {
return $selected->contains($value) ? 'selected' : null;
}

if (is_int($value) && is_bool($selected)) {
return (bool)$value === $selected;
}
return ((string) $value === (string) $selected) ? 'selected' : null;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ public function testSelect()
'<select name="countries"><option value="1" selected="selected">L</option><option value="2">M</option></select>',
$result
);

$select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']);
$this->assertEquals(
'<select name="avc"><option value="" hidden="hidden">Select</option><option value="1" selected>Yes</option><option value="0" >No</option></select>',
$select
);
}

public function testSelectCollection()
Expand Down

0 comments on commit 41cd929

Please sign in to comment.