phoenixframework / phoenix_html

Building blocks for working with HTML in Phoenix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Checkboxes generates same ID

ryvasquez opened this issue · comments

Hi Team 👋

First of all thanks for all great effort!

Using multiple checkbox/3 checkboxes emits warning from Liveview if you dont provide a custom ID, I also noticed that this doesnt happen when using multiple radio/4 due since appending the value to the ID

However I'm not sure if the team considering this scenario as valid, some form with multiple checkboxes can be grouped by name and pass the value as an array in the controller.

<.form let={f} for={@changeset} id="test-form">
    <%= checkbox(f, :famous, value: "A") %> Value A
    <%= checkbox(f, :famous, value: "B") %> Value B
</.form>

Will results to

<form action="#" method="post" id="test-form">
    <input name="_method" type="hidden" value="put">
    <input name="question[famous]" type="hidden" value="false">
    <input id="test-form_famous" name="question[famous]" type="checkbox" value="true"> Value A
    <input name="question[famous]" type="hidden" value="false">
    <input id="test-form_famous" name="question[famous]" type="checkbox" value="true"> Value B
</form>

And Liveview will throw error something like
Screen Shot 2022-05-04 at 7 32 41 PM

Please let me know if there is also same discussion happened before.
If the team thinks the case is valid, im happy to throw a Pull Request to address the said issue above.

Yes, this is expected, since the checkboxes are usually for "boolean" values. In the example above, one would typically use a radio button or a multiple select. But if you want to keep with checkboxes, then you need to pass the ID option :)

@josevalim I see, totally makes sense! Thankyou for the explanation, I will close this issue now 👍

@ryvasquez Ah ah, just saw this issue. FYI, this is fixed in #381 .

You'll need to have a name ending with [] though, and specify the checked_value (not value like in your code above), like:

checkbox(form, :search, :key,
               name: "search[key][]",
               checked_value: "first option",
              )
checkbox(form, :search, :key,
               name: "search[key][]",
               checked_value: "option 2",
                hidden_input: false
              )
[...]

@marcandre Thankyou very much! 👏