Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Multiple inputs named with [] #93

Closed
wants to merge 1 commit into from

Conversation

ftes
Copy link
Contributor

@ftes ftes commented Jun 18, 2024

Fixes #77
Replaces #79

@@ -124,6 +132,8 @@ defmodule PhoenixTest.Form do
defp to_form_field(name_element, value_element) do
name = Html.attribute(name_element, "name")
value = Html.attribute(value_element, "value")
list? = String.ends_with?(name, "[]")
value = if list?, do: [value], else: value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@totaltrash already mentioned Plug.Conn.Query.decode/4.

Would be great to reuse that to ensure we decode (nested) form data in the same way Phoenix and LiveView do.
Would get rid of some code here, and be more expressive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #94

ftes added a commit to ftes/phoenix_test that referenced this pull request Jun 18, 2024
germsvel pushed a commit that referenced this pull request Jul 5, 2024
Fixes #77
Replaces #79, #90, #93

## Goal
Ensure same form handling semantics as Phoenix controllers and LiveView.

## Details
Track form data as a flat list of `{name, value}` tuples instead of nested maps. 

This can be read straight from the DOM and needs no further conversions.

Example:
```ex
form_data = [
  {"user[name]", "Ada"},
  {"user[subscribe][]", "email"},
  {"user[subscribe][]", "push"}
]
```

Defer transforming that flat list into a nested data structure: When submitting form or evaluating other LiveLive `phx-*` callbacks.

Use [Plug.Conn.Query.decode/4](https://github.com/elixir-plug/plug/blob/v1.16.0/lib/plug/conn/query.ex#L9) to transform flat list into nested data structure. Reason: Phoenix Controllers and LiveView do the same (see #77 (comment)).
@germsvel
Copy link
Owner

germsvel commented Jul 5, 2024

@ftes I'm going to close this since we merged #94. I think this is no longer needed, right? If it is, let me know. Happy to reopen it.

@germsvel germsvel closed this Jul 5, 2024
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.

Support multiple checkboxes
2 participants