Skip to content

Commit 7ab381a

Browse files
authored
Merge pull request #14 from 8fold/forms
fix: Value could become integer
2 parents 82c7dfb + c2f81de commit 7ab381a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Forms/Select.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private function selectDropdown(): Element
125125
{
126126
$elements = [];
127127
foreach ($this->options as $value => $content) {
128+
$value = strval($value);
128129
$option = Element::option($content)->props('value ' . $value);
129130
if ($this->isSelected($value)) {
130131
$option = $option->prop('selected selected');
@@ -150,6 +151,7 @@ private function selectOther(): Element
150151
$type = 'checkbox';
151152
}
152153
foreach ($this->options as $value => $content) {
154+
$value = strval($value);
153155
$id = $this->name . '-' . $value;
154156
$label = Element::label($content)->props('for ' . $id);
155157
$input = Element::input()->omitEndTag()->props(

tests/Forms/SelectTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,25 @@ public function is_expected_base(): void // phpcs:ignore
118118

119119
$this->assertSame($expected, $result);
120120
}
121+
122+
/**
123+
* @test
124+
*/
125+
public function error_is_selected_value_always_string(): void // phpcs:ignore
126+
{
127+
$expected = <<<html
128+
<div><label for="select">Select your option</label><select id="select" name="select"><option value="0">display</option></select></div>
129+
html;
130+
131+
// Even with strict types, number-based keys become integers
132+
$result = (string) Select::create(
133+
'Select your option',
134+
'select',
135+
[
136+
'0' => 'display'
137+
]
138+
);
139+
140+
$this->assertSame($expected, $result);
141+
}
121142
}

0 commit comments

Comments
 (0)