KP123

KP123

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.

Showing Posts 1 to 3

KP123

KP123 OP

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.
CodeBloodedBiker

CodeBloodedBiker

How did you manage to solve this issue, especially for nested structures?
Even after following the fly.io article, I get the below error.

[debug] HANDLE EVENT "validate_form_field" in BrsWeb.FormLive.Index
  Component: BrsWeb.FormLive.FormComponent
  Parameters: %{"_target" => ["field", "fields", "fields_sort"], "field" => %{"fields" => %{"fields_sort" => ["on"]}, "label" => "", "name" => "", "type" => "panel"}}
[error] GenServer #PID<0.2705.0> terminating
** (Ecto.CastError) expected params to be a :map, got: `["on"]`
KP123

KP123 OP

Drop and sort need to be same level as the entity they’re operating on

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews