saulbensach

saulbensach

One modal to edit embeds_many in a live_view form

Hello everyone!,

I’m dealing with a LiveView from that includes a dynamic list using an embeds_many where each item can be edited through a modal. The modal itself is a LiveComponent with it’s own form. When pressing the “save” of the modal it sends the data back for that item using send/2

Under normal circumstances I run the following code

def handle_event("validate", params, socket) do
    changeset = FormState.changeset(socket.assigns.struct, params["form_state"])
    {:noreply, assign(socket, changeset: changeset)}
end

But because this modal is not inside the form I need to “patch” the incoming data in to the changeset manually.

# in case of a single item
params = %{
  "list_of_things" => %{
    form_index => %{"id" => id, "something" => something}
  }
}

FormState.change_list_of_things(socket.assigns.struct, params)

This becomes harder when the list contains more than one item, because I actually have to built the whole list of params so the cast_assoc can track properly the changes ad

The problem here is that I feel that having to build params manually with the indexes feels a bit odd, and makes me have several questions but I don’t actually know how to handle this situation.

  • Should I have a modal per item in the has_many?
  • Does the modal should somehow update the form_field and trigger the phx-change event?
  • Is there any more proper solution to this problem?

For me the solution is to go with the phoenix way by triggering params by phx-change event, but I don’t fully see a proper solution for this problem

Thanks for the help!

Edit: typo

First Post!

sodapopcan

sodapopcan

The modal itself is a LiveComponent with it’s own form

When you say it has its own form, do you mean form inputs that are part of a larger form, or each has its own <form>?

It would help too if you could share some heex code as I’m finding it a bit hard to picture exactly what you’re describing.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
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 Postg...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement