Skip to content

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

Open
banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/form-dropdown-post-array
Open

banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/form-dropdown-post-array

Conversation

@banghamid

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

This branch has not been deployed

No deployments
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.

1 participant