Skip to content
Open
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
8 changes: 4 additions & 4 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
if ($selected === []) {
$superglobals = service('superglobals');
if (is_array($data)) {
$postValue = $superglobals->post($data['name'] ?? '');
$postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data['name'] ?? ''));
if (isset($data['name']) && $postValue !== null) {
$selected = [$postValue];
$selected = is_array($postValue) ? $postValue : [$postValue];
}
} else {
$postValue = $superglobals->post($data);
$postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data));
if ($postValue !== null) {
$selected = [$postValue];
$selected = is_array($postValue) ? $postValue : [$postValue];
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,36 @@ public function testFormDropdownInferred(): void
service('superglobals')->unsetPost('cars');
}

public function testFormDropdownInferredArray(): void
{
$options = [
'Swedish Cars' => [
'volvo' => 'Volvo',
'saab' => 'Saab',
],
'German Cars' => [
'mercedes' => 'Mercedes',
'audi' => 'Audi',
],
];
$expected = <<<EOH
<select name="cars" multiple="multiple">
<optgroup label="Swedish Cars">
<option value="volvo" selected="selected">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi" selected="selected">Audi</option>
</optgroup>
</select>\n
EOH;
service('superglobals')->setPost('cars', ['volvo', 'audi']);
$this->assertSame($expected, form_dropdown('cars', $options));
$this->assertSame($expected, form_dropdown(['name' => 'cars'], $options));
service('superglobals')->unsetPost('cars');
}

public function testFormDropdownWithSelectedAttribute(): void
{
$expected = <<<'EOH'
Expand Down Expand Up @@ -684,6 +714,29 @@ public function testFormMultiselectArrayData(): void
$this->assertSame($expected, form_multiselect($data));
}

public function testFormMultiselectInferred(): void
{
$expected = <<<EOH
<select name="shirts[]" multiple="multiple">
<option value="small">Small Shirt</option>
<option value="med" selected="selected">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>\n
EOH;
$options = [
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
];

service('superglobals')->setPost('shirts', ['med', 'large']);
$this->assertSame($expected, form_multiselect('shirts[]', $options));
$this->assertSame($expected, form_multiselect(['name' => 'shirts[]', 'options' => $options]));
service('superglobals')->unsetPost('shirts');
}

public function testFormFieldset(): void
{
$expected = <<<EOH
Expand Down
Loading