-
Notifications
You must be signed in to change notification settings - Fork 963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Phoenix.Component.inputs_for/1 Ignores id and index from Phoenix.HTML.Form struct #3673
Comments
@woylie the persistent_id thing was added by Chris to correctly support reordering (#2570). The way I understand it is that it ensures that even when forms are re-ordered, the fields retain their original ID and therefore morphdom does not recreate them. I think we can change it to start at the given index without problems, but I don't think we can use the ID from the form, as that would introduce changing IDs again. When using inputs_for outside of LiveView, it wouldn't be a problem, so maybe there should be a way to opt out of _persistent_id? cc @chrismccord |
Starting at the given index would solve my use case.
Even within LiveView, reordering inputs isn't relevant for most filter forms. Neither is the tracking of unused fields (this was commented on here). Usually, we prefer to keep the filter parameters as query parameters in the URL. The addition of |
@SteffenDE My test unskipped in https://github.com/woylie/flop_phoenix/pull/430/files passes again with this change, so this appears to resolve the issue. Thank you! |
I posted this before on Elixir Forum before, but didn't get any response. I'm not sure whether this is a bug or whether there's a reason behind the current implementation, so please excuse me for posting the same question here.
Flop.Phoenix implements the
Phoenix.HTML.FormData
protocol for its meta struct: https://github.com/woylie/flop_phoenix/blob/main/lib/flop_phoenix/form_data.ex.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 thePhoenix.HTML.Form
struct are set into_form/4
:This worked fine with the old
inputs_for
function that was moved toPhoenixHTMLHelpers
. However, this stopped working withPhoenix.Component.inputs_for/1
. I can still pass the offset option (or any other option) to make it available in the form data implementation:And, given the offset
5
, the protocol implementation produces the correct values in theForm
struct: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_live_view/lib/phoenix_component.ex
Line 2736 in 8ea6b48
phoenix_live_view/lib/phoenix_component.ex
Line 2747 in 8ea6b48
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:
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. Shouldn'tinputs_for
retrieve everything it can from theForm
struct?The text was updated successfully, but these errors were encountered: