kuzyn

kuzyn

I am trying to create a LiveView form that allows 6 checkboxs to be toggled and saved as a single array of boolean values under Ecto.

The problem is that every time I change the form by clicking on the checkboxes, this array grows and new empty fields are added to the LV. As far as I can tell, it looks like it is appending a value before the validate handle_event. I cannot figure out why (I have another field, {:array. :float} that works just fine in the same view. Otherwise, i have nothing but boilerplate in my live component file (nothing added to mount, update, etc)…

What the bug looks like:
2024-11-25 17-33-19

In my context:

defmodule AppName.Things.MyThing do
  use Ecto.Schema
  import Ecto.Changeset

  schema "things" do
    field :my_list, {:array, :boolean}, default: [false, false, false, false, false, false]
    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(my_thing, attrs) do
    my_thing
    |> cast(attrs, [
      :included
    ])
  end
end

In my LV:

  @impl true
  def render(assigns) do
    ~H"""
    <div>
      <.simple_form
        for={@form}
        id="my_thing-form"
        phx-target={@myself}
        phx-change="validate"
        phx-submit="save"
      >
        <.input
          :for={
            {entry, index} <-
              Enum.with_index(Ecto.Changeset.get_field(@form.source, :my_list, []))
          }
          value={entry}
          name="my_thing[my_list][]"
          type="checkbox"
        />
        <:actions>
          <.button phx-disable-with="Saving...">Save My Thing</.button>
        </:actions>
      </.simple_form>
    </div>
    """
  end

Is this a problem from using :for on the checkbox input? I have tried a few things but they all felt hacky and did not work.

Showing Posts 1 to 2

frankdugan3

frankdugan3

This fly.io blog covers how to do it pretty well. Can vouch for it: I’ve based my checkbox group components on it.

kuzyn

kuzyn OP

Thanks Frank! My mistake was definitely not looking more into the limitations of the checkbox input in the core_component. See How to make checkbox work with form bindings Phoenix LV? - #2 by LostKobrakai.

For future reference, this thread is also related: How to wire up a Phoenix form for a field {:array, :string}?

What I ended up with is an ugly ass solution that works:

  • A :check_options assign initiated with existing my_list values (if present)
  • A handrolled vanilla <input type="checkbox" ...> inside of a :for div
  • A phx-change handler to handle the checkboxes on/off and sanitize them into my temporary checked_options in my assign
  • And finally a a Map.merge in my save handler that take this checked_options and put it into my params prior to generating the changeset

So in other words, I am binding the data manually with handlers and making sure it is merged into the params/changeset rather than relying on the field={@form[:my_list]} convenience. A whole lot uglier but at least I can move on :saluting_face:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews