fix(forms): preserve all values for repeated FormData keys on submit

Closed
ST
2mo ago
+297-2

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Closes #7342

What

The generated form submit handler builds form_data via
Object.fromEntries(new FormData($form).entries()), which silently keeps only the last value for
any key that appears more than once -- e.g. a checked rx.checkbox_group sharing one name.

Why

FormData.entries() yields one pair per repeated key by design; Object.fromEntries has no
special handling for that and just overwrites earlier pairs. There's no error or warning, so this
fails silently -- a checkbox group submits, form_data looks populated, and it's simply wrong.

How

Adds a formDataToObject helper (injected via Form.add_custom_code, mirroring the existing
add_hooks gate on EventTriggers.ON_SUBMIT) that groups repeated keys into an array while
leaving single-value keys as plain scalars, and uses it in _handle_submit_js_template in place of
Object.fromEntries(...entries()).

Note: #7227 is also open and touches this same _handle_submit_js_template block (a different,
unrelated bug -- filtering null values out of the ref-mapping merge). This PR is based on current
main before that lands; happy to rebase on whichever merges second.

Tests

Two new tests in tests/units/components/forms/test_form.py:

  • test_on_submit_emits_form_data_to_object_helper -- asserts the helper is injected into the page
    for any form with on_submit.
  • test_handle_submit_uses_form_data_to_object_not_fromentries -- asserts the generated hook calls
    formDataToObject(new FormData($form)) and does not contain Object.fromEntries, directly
    pinning the fix.

Full suite: uv run pytest tests/units --cov --no-cov-on-fail --cov-report= passes (4188/4188).
uv run ruff check . / uv run ruff format --check . / uv run pyright all clean on the changed
files.