Zesky665
Multiple select form with checkboxes
Hi,
Is there any standard way to implement a multiple select form with checkboxes in phoenix?
I know that there is a multi-select thingy in the Phoenix.HTML module, but that’s weird and I have no idea how or why anyone would use it.
Thanks
Marked As Solved
kokolegorille
I know it is possible because I have done that before with Rails and SimpleForm…
This example is a many_to_many relationship between User and Role. The point is to use simple input and specify the name of the checkbox as user[roles_ids][] with square brackets at the end.
<%= for role <- @roles do %>
<label>
<%= role.name %>:
<input name="user[roles_ids][]" type="checkbox" value="<%= role.id %>">
</label>
<% end %>
And that is the log from the request (with stripped csrf token) when I select roles 1 and 2.
[debug] Processing with DemoWeb.UserController.create/2
Parameters: %{"_utf8" => "✓", "user" => %{"name" => "hello", "roles_ids" => ["1", "2"]}}
Pipelines: [:browser]
As You can see, I get a list of ids ![]()
As a side note, it’s possible to add a check value to checkbox like this…
checked="<%= user_has_role?(@user, role)%>"
… as mentionned in this old post Many to many checkbox form - #4 by josevalim
Also Liked
benwilson512
Standard way would be to use the multi select helper from Phoenix.
Ooook. If you don’t want to use it, just write up the normal HTML.
mythicalprogrammer
You can only choose one option in radio button, at least in html.
It seems like OP wants to have check boxes so user can choose multiple options?
Zesky665
Out of curiosity does anyone use the multi_select input? I’ve seen it in the phoenix docs and on wc3 but nowhere else.
Is there something you’re supposed to do with it on the front end to make it look like a checkbox group or does it always look like a bugged out select?
Btw, I managed to make something like a checkbox group with the phoenix.form and some js but I doubt its best practice.








