Elixir phoenix inputs_for does not preserve source

117 Views Asked by At

I'm currently working on a form to allow adding/changing and removing users from a device. This is a live view and deleting sets the action on the changeset to :delete which is used later when saving. But i want to access this action in the inputs_for to change the background color of the item, issue is that the action is correct in the parent form, but wrong in the child form. Parent form:

%Phoenix.HTML.Form{
  source: #Ecto.Changeset<
    action: nil,
    changes: %{
      users_devices: [
        #Ecto.Changeset<action: :delete, changes: %{}, errors: [],
         data: #MyApp.UserDevice<>, valid?: true>
      ]
    },
    errors: [],
    data: #MyApp.Device<>,
    valid?: true
  >,
  ...
}

Child form:

%Phoenix.HTML.Form{
  source: #Ecto.Changeset<action: nil, changes: %{}, errors: [],
   data: #MyApp.UserDevice<>, valid?: true>,
  ...
}

Template:

<.form :let={f} for={@changeset} phx-submit="save" class="is-fullwidth">
  <table class="table is-fullwidth">
    <thead>
      <tr>
        <th><%= gettext("User") %></th>
        <th><%= gettext("Actions") %></th>
      </tr>
    </thead>
    <tbody>
      <.inputs_for :let={ff} field={f[:users_devices]}>
        <tr class={if ff.action == :delete, do: "has-background-danger-light"}>
          <td><%= ff.data.user.name %></td>
          <td>
            <button
              phx-click="remove_user"
              phx-value-user_id={ff.data.user_id}
              class="button is-danger is-light"
              type="button"
            >
              <span class="icon"><%= Brady.inline_svg("remove") %></span>
            </button>
          </td>
        </tr>
      </.inputs_for>
      <tr>
        <td colspan="2">
          <% users = [{"", nil} | Enum.map(@users, &{&1.name, &1.id})]%>
          <%= select_input(f, :add_user_id, users, "phx-change": "add_user") %>
        </td>
      </tr>
    </tbody>
  </table>
  <%= submit gettext("Save"), class: "button is-primary is-fullwidth" %>
</.form>

I'm not sure if this is a bug or if i'm missing something. I didn't find simulat issues and also found no other way of accessing the changeset action through the ff variable.

0

There are 0 best solutions below