Using multiple inputs_for for the same entity

July 21, 2022

⚠️ Warning

Since the release of Phoenix LiveView 0.18.0 this post is no longer relevant. The remainder of this post is left here for the sake of reference.


Make sure to pass an id for multiple inputs_for for the same entity

You can use multiple inputs_for for the same entity, just make sure to pass ids to ensure that Phoenix can diff the fields correctly.

# some-file.ex
def render(assigns) do
~H"""
  <.form for={@user_changeset} let={f} %>
    <%= inputs_for f, :friend, fn friend -> %>
      <!-- ... -->
    <% end %>
    <!-- ... -->
    <%= inputs_for f, :friend, [id: "secondary-friend-input"], fn friend -> %>
      <!-- ... -->
    <% end %>
  <% end %>
"""

© 2023, Built with ❤️ by Blake Dietz