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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement