I am building form with liveview component, where I need change value of some inputs, after user leave input. I’ve used phx-blur on this input and handle_event. Event happens, I change socket.assigns.form.params
. Other input will change correctly, but the one input with phx-blur will not. Anyone have any idea why?
I use .form and .input from the generated core_components.ex.
Can you share some code as I don’t quite understand. You’re saying blur does trigger handle_event
but then doesn’t?
As a shot in the dark, are you changing socket.assigns.form.params
directly? You definitely should not be doing that.
Thank you for fast response. I apologize for my lousy English.
Yes, I change socket.assigns.form.params
directly:
def handle_event("event_blur", _params, socket) do
socket =
assign(socket, :form, %{
socket.assigns.form
| params: %{socket.assigns.form.params | "url" => "new value", "title" => ""}
})
{:noreply, socket}
end
What is the recommended method for changing form values from event handlers?
Hey, sorry for the delay in reply now—I just checked out Elixir Forum quickly before going out and no worries re English! I was just a bit confused.
I’m still not 100% sure what you’re aiming to accomplish but I think you want something like this:
socket =
update(socket, :form, fn %{source: changeset} ->
changeset
|> Ecto.Changeset.put_change(:url, "new value")
|> Ecto.Changeset.put_change(:title, "")
|> to_form()
end)
It’s ok to access :params
but they shouldn’t be updated as they are meant to represent the data given on changeset creation which is uncast data, ie, its contents are not be trusted.
It works. Thanks a lot.