diff --git a/src/Html/FormBuilder.php b/src/Html/FormBuilder.php index af5acdb01..e4864da9c 100644 --- a/src/Html/FormBuilder.php +++ b/src/Html/FormBuilder.php @@ -412,6 +412,7 @@ public function select(string $name, array $list = [], string|array|null $select { if (array_key_exists('emptyOption', $options)) { $list = ['' => $options['emptyOption']] + $list; + unset($options['emptyOption']); } // When building a select box the "value" attribute is really the selected one diff --git a/tests/Html/FormBuilderTest.php b/tests/Html/FormBuilderTest.php index 2432b00a4..d5bcd7403 100644 --- a/tests/Html/FormBuilderTest.php +++ b/tests/Html/FormBuilderTest.php @@ -319,4 +319,25 @@ public function testButtonSubmitType() $this->assertElementAttributeEquals('type', 'submit', $result); $this->assertElementContainsText('Apply', $result); } + + /** + * @testdox can create a select element with an empty option without emptyOption appearing as an attribute. + */ + public function testSelectWithEmptyOption() + { + $result = $this->formBuilder->select( + name: 'my-select', + list: ['1' => 'Option 1', '2' => 'Option 2'], + selected: null, + options: ['emptyOption' => 'Please select', 'class' => 'form-control'] + ); + + $this->assertElementIs('select', $result); + $this->assertElementAttributeEquals('name', 'my-select', $result); + $this->assertElementAttributeEquals('class', 'form-control', $result); + $this->assertElementDoesntHaveAttribute('emptyOption', $result); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('', $result); + } }