KP123

KP123

Live View Error With Cast Embed

Context: I’m building an Echo schema that embeds_one schema which itself contains an embeds_many field. I need to allow users to add and remove from the embeds_many field. I followed the tutorial provided in the Phoenix.Component.inputs_for/1 documentation hoping to take advantage of Ecto.Changeset.cast_embed’s :sort_param/:drop_param.

Problem: When embeds are cast, the drop param gets treated as a new embedded entity and gets passed to changeset for validation. Of course this crashes every time the form is modified as drop param is a list of indexes and not a map of user input data. The docs linked above state:

Outside the inputs_for , we render an empty mailing_list[emails_drop][] input, to ensure that all children are deleted when saving a form where the user dropped all entries. This hidden input is required whenever dropping associations.

In my case since this is an embeds_many inside of an embeds_one, my empty input looks like this patron[menu_item][modifications][mods_drop][]. When the empty form is validated the value [""] gets passed to my changeset function. When delete is pressed on a populated form, the index gets passed to the changeset function like so ["1"] (populated in mount, can’t edit form in UI as it crashes on every change).

The data passed to LiveView on change looks like so:

%{
  "_persistent_id" => "0",
  "item_name" => "",
  "modifications" => %{
    "0" => %{
      "_persistent_id" => "0",
      "comment" => ""
    },
    "mods_drop" => [""],
    "mods_sort" => ["0"]
  }
}

If mods_drop => ["3"] then ["3"] gets passed to the modifications changeset. If I comment out the hidden mods_drop input, the same problem occurs, this time on the mods_sort input. I feel I must be missing something crucial in the docs but I’m not sure what.

First Post!

KP123

KP123

I realized that in the documentation snippet that I shared above, does not nest emails_drop under [emails]. Applying that to my own input name led to me changing patron[menu_item][modifications][mods_drop][] to patron[menu_item][mods_drop][]. After making that change the crashing mentioned above ceased but a new bug appeared, the UI didn’t react at all. It wasn’t until I abandoned the button based approach from the Phoenix.Component.inputs_for/1 documentation and replaced it with the label/checkbox approach outlined in this fly.io article that I managed to get results I wanted.

Edit: After getting the checkbox version working I did revisit the button version to ensure it was not my error. It never worked for me

Solution:

  • Abandon the button based approach from the inputs_for/1 docs entirely
  • Be careful when hand writing strings, especially for nested structures. Perhaps a future version of inputs_for could generate this name for me like it does for form indexes. Then I could manually apply the proper value where I need it.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

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
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement