woylie

woylie

Question about _persistent_id in Phoenix LiveView's inputs_for component

Flop.Phoenix implements the Phoenix.HTML.FormData protocol for its meta struct: flop_phoenix/lib/flop_phoenix/form_data.ex at main · woylie/flop_phoenix · GitHub

At some point, an offset option was added to allow users to build more complex form layouts. By setting the option, the :index, :id, and :name fields on the Phoenix.HTML.Form struct are set in to_form/4:

offset_string = Integer.to_string(offset)

%Phoenix.HTML.Form{
  index: offset,
  id: id <> "_" <> offset_string,
  name: name <> "[" <> offset_string <> "]",
  # ...
}

This worked fine with the old inputs_for function that was moved to PhoenixHTMLHelpers. However, this stopped working with Phoenix.Component.inputs_for/1. I can still pass the offset option (or any other option) to make it available in the form data implementation:

~H"""
<.inputs_for
  :let={ff}
  field={@form[:filters]}
  options={[fields: @fields, offset: 5]}
>
  <%!-- ... %>
</.inputs_for>
"""

And, given the offset `5`, the protocol implementation produces the correct values in the `Form` struct:

```elixir
%Phoenix.HTML.Form{
  id: "flop_filters_5",
  name: "filters[5]",
  index: 5,
  # ...
}

However, Phoenix.Component.inputs_for/1 appears to ignore both the index and the id that the protocol implementation produces. The form structs passed to the inner block always start with index 0 and an according ID (although the name is derived correctly from the form struct returned by the protocol implementation):

%Phoenix.HTML.Form{
  id: "flop_filters_0",
  name: "filters[5]",
  hidden: [{"_persistent_id", "0"}, {:field, :email}],
  params: %{"_persistent_id" => "0"},
  index: 0
}

I noticed that I can initialize the params on the form struct produced by my protocol implementation with a _persistent_id to achieve the previous behavior:

%Phoenix.HTML.Form{
  index: index,
  id: id <> "_" <> index_string,
  name: name <> "[" <> index_string <> "]",
  params: %{"_persistent_id" => index}
  # ...
}

This leads to the correct index being used by inputs_for, but this relies on an internal implementation detail and probably isn’t meant to be used this way.

I don’t know why it was implemented this way, and whether this was an oversight, or what the purpose of the _persistent_id is, or whether I’m doing something wrong. Can someone more familiar with LiveView give me some insights?

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement