Skip to content

Commit 20a7022

Browse files
committed
fix(Helpers): handle array POST values when inferring selected options in form_dropdown()
1 parent 947a7fe commit 20a7022

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

‎system/Helpers/form_helper.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,14 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
295295
if ($selected === []) {
296296
$superglobals = service('superglobals');
297297
if (is_array($data)) {
298-
$postValue = $superglobals->post($data['name'] ?? '');
298+
$postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data['name'] ?? ''));
299299
if (isset($data['name']) && $postValue !== null) {
300-
$selected = [$postValue];
300+
$selected = is_array($postValue) ? $postValue : [$postValue];
301301
}
302302
} else {
303-
$postValue = $superglobals->post($data);
303+
$postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data));
304304
if ($postValue !== null) {
305-
$selected = [$postValue];
305+
$selected = is_array($postValue) ? $postValue : [$postValue];
306306
}
307307
}
308308
}

‎tests/system/Helpers/FormHelperTest.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,36 @@ public function testFormDropdownInferred(): void
588588
service('superglobals')->unsetPost('cars');
589589
}
590590

591+
public function testFormDropdownInferredArray(): void
592+
{
593+
$options = [
594+
'Swedish Cars' => [
595+
'volvo' => 'Volvo',
596+
'saab' => 'Saab',
597+
],
598+
'German Cars' => [
599+
'mercedes' => 'Mercedes',
600+
'audi' => 'Audi',
601+
],
602+
];
603+
$expected = <<<EOH
604+
<select name="cars" multiple="multiple">
605+
<optgroup label="Swedish Cars">
606+
<option value="volvo" selected="selected">Volvo</option>
607+
<option value="saab">Saab</option>
608+
</optgroup>
609+
<optgroup label="German Cars">
610+
<option value="mercedes">Mercedes</option>
611+
<option value="audi" selected="selected">Audi</option>
612+
</optgroup>
613+
</select>\n
614+
EOH;
615+
service('superglobals')->setPost('cars', ['volvo', 'audi']);
616+
$this->assertSame($expected, form_dropdown('cars', $options));
617+
$this->assertSame($expected, form_dropdown(['name' => 'cars'], $options));
618+
service('superglobals')->unsetPost('cars');
619+
}
620+
591621
public function testFormDropdownWithSelectedAttribute(): void
592622
{
593623
$expected = <<<'EOH'
@@ -684,6 +714,29 @@ public function testFormMultiselectArrayData(): void
684714
$this->assertSame($expected, form_multiselect($data));
685715
}
686716

717+
public function testFormMultiselectInferred(): void
718+
{
719+
$expected = <<<EOH
720+
<select name="shirts[]" multiple="multiple">
721+
<option value="small">Small Shirt</option>
722+
<option value="med" selected="selected">Medium Shirt</option>
723+
<option value="large" selected="selected">Large Shirt</option>
724+
<option value="xlarge">Extra Large Shirt</option>
725+
</select>\n
726+
EOH;
727+
$options = [
728+
'small' => 'Small Shirt',
729+
'med' => 'Medium Shirt',
730+
'large' => 'Large Shirt',
731+
'xlarge' => 'Extra Large Shirt',
732+
];
733+
734+
service('superglobals')->setPost('shirts', ['med', 'large']);
735+
$this->assertSame($expected, form_multiselect('shirts[]', $options));
736+
$this->assertSame($expected, form_multiselect(['name' => 'shirts[]', 'options' => $options]));
737+
service('superglobals')->unsetPost('shirts');
738+
}
739+
687740
public function testFormFieldset(): void
688741
{
689742
$expected = <<<EOH

0 commit comments

Comments
 (0)