From c2f81deb4c95ae671d82fd7d2d8434bf69ef62ae Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Sat, 18 Mar 2023 14:26:33 -0400 Subject: [PATCH] fix: Value could become integer --- src/Forms/Select.php | 2 ++ tests/Forms/SelectTest.php | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Forms/Select.php b/src/Forms/Select.php index a3eb455..14f48d6 100644 --- a/src/Forms/Select.php +++ b/src/Forms/Select.php @@ -125,6 +125,7 @@ private function selectDropdown(): Element { $elements = []; foreach ($this->options as $value => $content) { + $value = strval($value); $option = Element::option($content)->props('value ' . $value); if ($this->isSelected($value)) { $option = $option->prop('selected selected'); @@ -150,6 +151,7 @@ private function selectOther(): Element $type = 'checkbox'; } foreach ($this->options as $value => $content) { + $value = strval($value); $id = $this->name . '-' . $value; $label = Element::label($content)->props('for ' . $id); $input = Element::input()->omitEndTag()->props( diff --git a/tests/Forms/SelectTest.php b/tests/Forms/SelectTest.php index c478a1e..ea22a14 100644 --- a/tests/Forms/SelectTest.php +++ b/tests/Forms/SelectTest.php @@ -118,4 +118,25 @@ public function is_expected_base(): void // phpcs:ignore $this->assertSame($expected, $result); } + + /** + * @test + */ + public function error_is_selected_value_always_string(): void // phpcs:ignore + { + $expected = << + html; + + // Even with strict types, number-based keys become integers + $result = (string) Select::create( + 'Select your option', + 'select', + [ + '0' => 'display' + ] + ); + + $this->assertSame($expected, $result); + } }