Skip to content

Commit e2b62bd

Browse files
authored
fix(Helpers): handle array POST values when inferring selected options in form_dropdown() (#10755)
1 parent 15f9c86 commit e2b62bd

3 files changed

Lines changed: 86 additions & 10 deletions

File tree

‎docs/helpers/form_helper.md‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ echo form_dropdown('shirts', $options, 'large', $js);
461461

462462
If the array passed as `$options` is a multidimensional array, then `form_dropdown()` will produce an <optgroup> with the array key as the label.
463463

464+
##### Automatic Selection from POST
465+
466+
If no value is passed to `$selected` (or if it is an empty array), `form_dropdown()` and `form_multiselect()` will attempt to infer the selected option(s) directly from the current request's `$_POST` data using the field name (trailing `[]` brackets are automatically stripped).
467+
468+
!!! note "Note"
469+
This fallback only inspects `$_POST` from the current request. It does not restore old input after a redirect with `redirect()->withInput()`. To restore submitted values after a redirect, pass `set_value()` or `old()` to `$selected` explicitly.
470+
464471
#### form_multiselect([$name = ''[, $options = [][, $selected = [][, $extra = '']]]])
465472

466473
- **Parameters**
@@ -475,7 +482,25 @@ If the array passed as `$options` is a multidimensional array, then `form_dropdo
475482

476483
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected.
477484

478-
The parameter usage is identical to using `form_dropdown()` above, except, of course, that the name of the field will need to use POST array syntax, e.g., foo[].
485+
The parameter usage is identical to using `form_dropdown()` above, except, of course, that the name of the field will need to use POST array syntax, e.g., `shirts[]`.
486+
487+
If `$selected` is omitted, selections are automatically inferred from `$_POST['shirts']`:
488+
489+
```php
490+
<?php
491+
492+
// If $_POST['shirts'] contains ['small', 'large']:
493+
echo form_multiselect('shirts[]', $options);
494+
/*
495+
* Would produce:
496+
* <select name="shirts[]" multiple="multiple">
497+
* <option value="small" selected="selected">Small Shirt</option>
498+
* <option value="med">Medium Shirt</option>
499+
* <option value="large" selected="selected">Large Shirt</option>
500+
* <option value="xlarge">Extra Large Shirt</option>
501+
* </select>
502+
*/
503+
```
479504

480505
#### form_fieldset([$legend_text = ''[, $attributes = []]])
481506

‎system/Helpers/form_helper.php‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,14 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
293293

294294
// If no selected state was submitted we will attempt to set it automatically
295295
if ($selected === []) {
296-
$superglobals = service('superglobals');
297-
if (is_array($data)) {
298-
$postValue = $superglobals->post($data['name'] ?? '');
299-
if (isset($data['name']) && $postValue !== null) {
300-
$selected = [$postValue];
301-
}
302-
} else {
303-
$postValue = $superglobals->post($data);
296+
$name = is_array($data) ? ($data['name'] ?? '') : $data;
297+
$name = str_ends_with($name, '[]') ? substr($name, 0, -2) : $name;
298+
299+
if ($name !== '') {
300+
$postValue = service('superglobals')->post($name);
301+
304302
if ($postValue !== null) {
305-
$selected = [$postValue];
303+
$selected = is_array($postValue) ? $postValue : [$postValue];
306304
}
307305
}
308306
}

‎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)