Skip to content

Fix: Omit hidden input before checkbox group - #314

Merged
germsvel merged 3 commits into
germsvel:mainfrom
ftes:fix/hidden-checkbox-group-input-order
Aug 2, 2026
Merged

Fix: Omit hidden input before checkbox group#314
germsvel merged 3 commits into
germsvel:mainfrom
ftes:fix/hidden-checkbox-group-input-order

Conversation

@ftes

@ftes ftes commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Omit hidden 'reset' input that appears before checkbox group if any checkboxes are checked.

What changed?

  • Better preserve DOM order of form elements better when building form data.

Why make these changes?

A hidden input before a checkbox group can be used to reset the value if all checkboxes are unchecked.
However, if checkboxes are checked, then the hidden input should be effectively ignored.
Plug/Phoenix can handle this if the field values arrive in DOM order.

In forms like:

<input type="hidden" name="list" value="" />
<input type="checkbox" name="list[]" value="one" />
<input type="checkbox" name="list[]" value="two" />

PhoenixTest previously collected controls by selector category and stored FormData in a map. That lost DOM order and collapsed repeated field names, which could cause the hidden scalar entry to interfere with the checkbox array payload.

This PR is only a partial improvement. The ideal solution would be to submit form data in the exact DOM order a browser would, end to end.

@ftes
ftes force-pushed the fix/hidden-checkbox-group-input-order branch from 22300cc to 462f68f Compare April 29, 2026 05:07
Comment thread lib/phoenix_test/element/form.ex Outdated
nil ->
data ++ new_entries

first_index ->

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a bit of a crux to handle multiple fields with same name.

@ftes
ftes marked this pull request as ready for review April 29, 2026 06:17
@germsvel

germsvel commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Thanks for the work @ftes! I was really hoping we wouldn't have to revert back to lists for FormData, but it makes sense that we want to preserve that order. I can't recall now how we were preserving it before.

To your comment:

The ideal solution would be to submit form data in the exact DOM order a browser would, end to end.

I've been wondering if there's a way to do that. In LiveViews, we might not need to keep track of FormData. In dead views, maybe? But even then, I wonder if when we submit the form, we don't use FormData, but rather whatever is on the page itself.

In any case, this is fixing an issue, and doesn't seem to break other things, so I'm gonna go ahead and merge it.

@germsvel
germsvel merged commit ff122ce into germsvel:main Aug 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants