Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to make checkbox work with form bindings Phoenix LV?

Background

I have a LiveView page where I have a small form. This form is supposed ton have a group of radio buttons, and a group of checkboxes.

To achieve this, I am trying to use Form Bindings:

Code

This is what I currently have. I want to give the user the option to receive notifications about some topics.

For the radio button group, the user chooses whether to receive the notification via email or sms. These are mutually exclusive.

For the checkbox group, the user chooses the topics. These are not mutually exclusive.

This is my html.heex file:

    <.form for={@form} phx-submit="save">
       <.input id={"email"} name="notifications" type="radio" value={"email"} field={@form[:notifications]} />
       <.input id={"sms"} name="notifications" type="radio" value={"sms"} field={@form[:notifications]} />

      <.input id={"sports"} name="topics" type="checkbox" value={"sports"} field={@form[:topics]} />
      <.input id={"science"} name="topics" type="checkbox" value={"science"} field={@form[:topics]} />

      <button>Save</button>
    </.form>

And this is the corresponding LivewView:

defmodule MyApp.Settings do
  use MyApp, :live_view

  @impl true
  def mount(_params, _session, socket) do
  
    form = to_form(%{})
    updated_socket = assign(socket, form: form)

    {:ok, updated_socket}
  end

  def handle_event(event, params, socket) do
    Logger.info("Event: #{inspect(event)} ; #{inspect(params)}")
    {:noreply, socket}
  end

end

If you are a keen reader, you will see I have no label, or legend tags anywhere. This is for simplification purposes.

Problem

Even though I get the correct value for the "notifications" radio group, the problem is that my checkboxes always return true or false:

 [debug] HANDLE EVENT "save" in MyApp.Settings
  Parameters: %{"notifications" => "sms", "topics" => "true"}

This is rather confusing. I was expecting something like:

Parameters: %{"notifications" => "sms", "topics" => ["sports", "science"]}

After reading the relevant parts of the docs, I don’t think the type checkbox is considered a special type, so it is not documented (in the link above mentioned).

I also don’t quite understand why I need a schema when to_form seems to work perfectly fine with %{}.

Questions

  • How can I get a list of values instead of a boolean for checkboxes?
  • I don’t use schemas in my application. Can to_form work with a Struct?
  • What would be the benefit of passing a Struct to to_form ?

Marked As Solved

Fl4m3Ph03n1x

Fl4m3Ph03n1x

After reading both Articles provided by @LostKobrakai I have arrived to a code that mixes what I learned from both. Definitely recommend the reading. Here are the code pieces for your inspiration:

core_components.ex

  @doc """
  Generate a checkbox group for multi-select.

  ## Examples

    <.checkgroup
      field={@form[:genres]}
      label="Genres"
      options={[%{name: "Fantasy", id: "fantasy"}, %{name: "Science Fiction", id: "sci-fi"}]}
      selected={[%{name: "Fantasy", id: "fantasy"}]}
    />

  """
  attr :id, :any
  attr :name, :any
  attr :label, :string, default: nil
  attr :field, Phoenix.HTML.FormField, doc: "a form field struct retrieved from the form, for example: @form[:genres]"
  attr :errors, :list
  attr :required, :boolean, default: false
  attr :options, :list, doc: "the options to pass to Phoenix.HTML.Form.options_for_select/2"
  attr :rest, :global, include: ~w(disabled form readonly)
  attr :class, :string, default: nil

  attr :selected, :any, default: [],
    doc: "the currently selected options, to know which boxes are checked"

  def checkgroup(assigns) do
    new_assigns =
      assigns
      |> assign(:multiple, true)
      |> assign(:type, "checkgroup")

    input(new_assigns)
  end



  def input(%{type: "checkgroup"} = assigns) do
    ~H"""
    <div class="mt-2">
      <%= for opt <- @options do %>

        <div class="relative flex gap-x-3">
          <div class="flex h-6 items-center">
            <input id={opt.id} name={@name} type="checkbox" value={opt.id} class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-600" checked={opt in @selected}>
          </div>
          <div class="text-sm leading-6">
            <label  for={opt.id} class="text-base font-semibold text-gray-900"><%= opt.name %></label>
          </div>
        </div>

      <% end %>
    </div>
    """
  end

usage_live.html.heex

      <.simple_form for={@form} phx-change="change" phx-submit="execute">

        <div class="mt-4">
          <.checkgroup field={@form[:genres]} label="Genres" options={@all_genres} selected={@selected_genres} required/>
        </div>

        <div class="mt-4">
          <.button}>Execute Command</.button>
        </div>
      </.simple_form>

Also Liked

LostKobrakai

LostKobrakai

The built in <.input type="checkbox"> is for selfstanding checkboxes (boolean on the backend), not for multi value fields. Phoenix doesn’t come with helpers for a set of checkboxes.

See Making a CheckboxGroup Input · The Phoenix Files or https://kobrakai.de/scratchpad/checkboxes on approaches to handling such.

Where Next?

Popular in Questions Top

_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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics 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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement