Skip to content

Commit 3de97f6

Browse files
mjauvinLukeTowers
andauthored
Remove emptyOption after using it to fill the options array (#221)
Co-authored-by: Luke Towers <[email protected]>
1 parent deb4304 commit 3de97f6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Html/FormBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ public function select(string $name, array $list = [], string|array|null $select
412412
{
413413
if (array_key_exists('emptyOption', $options)) {
414414
$list = ['' => $options['emptyOption']] + $list;
415+
unset($options['emptyOption']);
415416
}
416417

417418
// When building a select box the "value" attribute is really the selected one

tests/Html/FormBuilderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,25 @@ public function testButtonSubmitType()
319319
$this->assertElementAttributeEquals('type', 'submit', $result);
320320
$this->assertElementContainsText('Apply', $result);
321321
}
322+
323+
/**
324+
* @testdox can create a select element with an empty option without emptyOption appearing as an attribute.
325+
*/
326+
public function testSelectWithEmptyOption()
327+
{
328+
$result = $this->formBuilder->select(
329+
name: 'my-select',
330+
list: ['1' => 'Option 1', '2' => 'Option 2'],
331+
selected: null,
332+
options: ['emptyOption' => 'Please select', 'class' => 'form-control']
333+
);
334+
335+
$this->assertElementIs('select', $result);
336+
$this->assertElementAttributeEquals('name', 'my-select', $result);
337+
$this->assertElementAttributeEquals('class', 'form-control', $result);
338+
$this->assertElementDoesntHaveAttribute('emptyOption', $result);
339+
$this->assertStringContainsString('<option value="">Please select</option>', $result);
340+
$this->assertStringContainsString('<option value="1">Option 1</option>', $result);
341+
$this->assertStringContainsString('<option value="2">Option 2</option>', $result);
342+
}
322343
}

0 commit comments

Comments
 (0)