Zesky665

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

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 :slight_smile:

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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

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.

Last Post!

nathanl

nathanl

Also, according to Making a CheckboxGroup Input · The Phoenix Files

The multiple option is processed in the default generated input/1 function

which adds [] to the input name, so that name="tags" becomes name=tags[] if you passed multiple=true. I don’t see that in a quick perusal of the docs, but maybe that’s because it’s in a generated <.input function rather than a library function.

But what you said is the key.

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement