nyakio

nyakio

Multiselect checkbox input for existing records

I have an invitation form that includes a list of existing groups a user can be invited to. The form does not allow creating new groups but lets users select from the existing groups to associate with the invitation.
Below is a sample form input:

Additionally, here is the Invitation#create action:

actions do
    defaults [:read, :destroy]

    create :create do
      accept [:email, :language]

      argument :groups, {:array, :uuid}, allow_nil?: false

      change manage_relationship(:groups,
               on_lookup: :relate,
               on_no_match: :error,
               on_match: :ignore,
               on_missing: :unrelate
             )

      primary? true
    end
  end

What is the best way to create an input field that allows selecting existing groups to associate with an invitation?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Hey there! I think the most straightforward way to go about this is to use the prepare_params option of AshPhoenix.Form. I put together a little example project that you can run, and go to /posts. There you can create a post with two category options each toggled with checkboxes. In your case, the label would be the name of the group, and instead of #{category} it would be something like #{group.id}.

This is a link to the relevant form_component.ex.

https://github.com/zachdaniel/checkbox_example/blob/main/lib/example_web/live/post_live/form_component.ex

Also Liked

almirsarajcic

almirsarajcic

For anyone struggling with this, we solved it by getting the value of groups:

def render(assigns) do
  assigns =
    assigns
    |> assign(:checked_groups, AshPhoenix.Form.value(assigns.form, :groups) || [])

and setting checked attribute depending on whether a group is inside that list:

<%= for group <- @groups do %>
  <.input
    type="checkbox"
    label={group.name}
    name={@form.name <> "[groups][#{group.id}]"}
    id={@form.id <> "_groups_#{group.id}"}
    checked={group.id in @checked_groups}
    />

The groups stay checked after validation.
We just need to make sure we’re assigning the form during validation, as always:

def handle_event("validate", %{"invitation" => params}, socket) do
  form = AshPhoenix.Form.validate(socket.assigns.form, params)
  {:noreply, assign(socket, :form, form)}
end

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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

We're in Beta

About us Mission Statement