From 20a7022d39edaea09317533016033ec0eea2cb1a Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:08:18 +0700 Subject: [PATCH 1/2] fix(Helpers): handle array POST values when inferring selected options in form_dropdown() --- system/Helpers/form_helper.php | 8 ++-- tests/system/Helpers/FormHelperTest.php | 53 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index b030b2f06009..465da04f6034 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -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]; } } } diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 7ed8c8668123..bfd3d12e0efe 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -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 = << + + + + + + + + + \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' @@ -684,6 +714,29 @@ public function testFormMultiselectArrayData(): void $this->assertSame($expected, form_multiselect($data)); } + public function testFormMultiselectInferred(): void + { + $expected = << + + + + + \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 = << Date: Fri, 13 Nov 2026 20:18:26 +0700 Subject: [PATCH 2/2] refactor(Helpers): simplify name extraction and document POST inference --- docs/helpers/form_helper.md | 27 ++++++++++++++++++++++++++- system/Helpers/form_helper.php | 14 ++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/docs/helpers/form_helper.md b/docs/helpers/form_helper.md index 52775f83b864..c3c793417452 100644 --- a/docs/helpers/form_helper.md +++ b/docs/helpers/form_helper.md @@ -461,6 +461,13 @@ echo form_dropdown('shirts', $options, 'large', $js); If the array passed as `$options` is a multidimensional array, then `form_dropdown()` will produce an <optgroup> with the array key as the label. +##### Automatic Selection from POST + +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). + +!!! note "Note" + 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. + #### form_multiselect([$name = ''[, $options = [][, $selected = [][, $extra = '']]]]) - **Parameters** @@ -475,7 +482,25 @@ If the array passed as `$options` is a multidimensional array, then `form_dropdo 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. -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[]. +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[]`. + +If `$selected` is omitted, selections are automatically inferred from `$_POST['shirts']`: + +```php + + * + * + * + * + * + */ +``` #### form_fieldset([$legend_text = ''[, $attributes = []]]) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 465da04f6034..19cc8eb949b9 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -293,14 +293,12 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''): // If no selected state was submitted we will attempt to set it automatically if ($selected === []) { - $superglobals = service('superglobals'); - if (is_array($data)) { - $postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data['name'] ?? '')); - if (isset($data['name']) && $postValue !== null) { - $selected = is_array($postValue) ? $postValue : [$postValue]; - } - } else { - $postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data)); + $name = is_array($data) ? ($data['name'] ?? '') : $data; + $name = str_ends_with($name, '[]') ? substr($name, 0, -2) : $name; + + if ($name !== '') { + $postValue = service('superglobals')->post($name); + if ($postValue !== null) { $selected = is_array($postValue) ? $postValue : [$postValue]; }