Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Html/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/Html/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<option value="">Please select</option>', $result);
$this->assertStringContainsString('<option value="1">Option 1</option>', $result);
$this->assertStringContainsString('<option value="2">Option 2</option>', $result);
}
}