I’m not sure what you mean, but what I’m doing seems so cumbersome for something seemingly so simple, that I feel like I’m missing something. Let me give some more info…
Here’s my link html:
<%= link "clear", to: "#", "phx-click": "clear-time", "phx-value-id": time_form.index + 1 %>
Here’s what my form looks like visually:
Here’s my event handler:
def handle_event("clear-time", args, socket) do
%{ "id" => id } = args
params = %{
"times" => %{
"1" => %{"id" => "1"},
"2" => %{"id" => "2"}
}
}
params = update_in(params["times"][id], fn time ->
Map.merge(time, %{"start_time" => "", "end_time" => ""})
end)
changeset = socket.assigns.changeset
|> Ecto.Changeset.cast(params, [])
|> Ecto.Changeset.cast_embed(:times, with: &TimedNode.Changeset.create_times/2)
|> TimedNode.Changeset.validate_times()
socket = assign(socket, :changeset, changeset)
{:noreply, socket}
end
Which only kind of works. I get duplicate validation errors click on the clear link twice, among other weird issues regarding state getting out of whack.
Contrast that to how trivial it would be if I had all the form params in the event handler:
def handle_event("clear-time", args, socket) do
%{ "id" => id, "params" => params } = args
params = put_in(params["times"][id]["start_time"], nil)
params = put_in(params["times"][id]["end_time"], nil)
changeset = TimedNode.Changeset.update(socket.assigns.timed_node, params)
socket = assign(socket, :changeset, changeset)
{:noreply, socket}
end
In other words, I could just create a brand new changeset with all the params, rather than trying to update my changeset somehow.
Thanks for the help.