Skip to content

fix(Helpers): handle array POST values when inferring selected options in form_dropdown() - #10755

Merged
michalsn merged 2 commits into
codeigniter4:developfrom
banghamid:fix/form-dropdown-post-array
Nov 13, 2026
Merged

michalsn merged 2 commits into
codeigniter4:developfrom
banghamid:fix/form-dropdown-post-array

Conversation

@banghamid

@banghamid banghamid commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description
When form_dropdown() or form_multiselect() is called without an explicit $selected argument, form_dropdown() attempts to infer the selected option(s) from $_POST via service('superglobals')->post().

Previously:

  1. When the $_POST entry for the field was an array (for example, from a multiple-select submission), $selected = [$postValue] wrapped the array inside another array ([['volvo', 'audi']]), causing the subsequent (string) $item cast to throw an unhandled ErrorException: Array to string conversion.
  2. When field names used POST array notation (e.g., shirts[] as required by form_multiselect()), post('shirts[]') did not strip the trailing [] to look up $_POST['shirts'].

This PR:

  • Preserves array $postValue values via is_array($postValue) ? $postValue : [$postValue] instead of wrapping them in a nested array.
  • Strips trailing [] from the field name when looking up the inferred value in $_POST.
  • Adds regression tests in FormHelperTest (testFormDropdownInferredArray() and testFormMultiselectInferred()).

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 10, 2026
Comment thread system/Helpers/form_helper.php Resolved
Comment thread system/Helpers/form_helper.php Resolved
@banghamid
banghamid force-pushed the fix/form-dropdown-post-array branch from e337ae7 to 252a386 Compare November 13, 2026 13:18
@banghamid

banghamid commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review @michalsn!

Pushed an update addressing both points:

  • Extracted \ first and replaced regex with str_ends_with(, '[]') / substr(, 0, -2), sharing the POST lookup logic across string and array .
  • Documented the automatic selection fallback from \ in docs/helpers/form_helper.md with an example using shirts[], and clarified that it only inspects \ directly and does not restore flashed input after
    edirect()->withInput().

@banghamid
banghamid requested a review from michalsn November 13, 2026 13:36

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you!

@michalsn
michalsn merged commit e2b62bd into codeigniter4:develop Nov 13, 2026
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants