I have 2 fields in a form with liveview
which receive a value through the socket
What happens to me is that when I modify a field and move to the next one, the value of the field that I already modified returns to the one it had previously
which doesn’t allow me to change 2 fields at the same time
To be able to handle form changes, you should use the phx-change event. Then you need to create callback that will be invoked to handle events sent by the client. In the callback function body you will probably perform some validation and assign value to socket. You can take a look here
I am using phx-change… what happens is that my program brings parameters to auto-fill the fields so that I can modify them later
(in this case the fields are name and duration)
previously I did it in a live_component in the following way
@impl true
def update(%{prestacion: prestacion} = assigns, socket) do
changeset = Prestaciones.change_prestacion(prestacion)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"prestacion" => prestacion_params}, socket) do
changeset =
socket.assigns.prestacion
|> Prestaciones.change_prestacion(prestacion_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("edit", %{"prestacion" => prestacion_params}, socket) do
prestacion_id = 74
recurso_prestaciones = HelloWorld.Prestaciones.get_recursos(prestacion_id)
save_prestacion(
socket,
socket.assigns.action,
prestacion_params,
socket.assigns.selected_rows
)
end
clarification:
what i use in the phx-change is
def handle_event(“validate”)
What I am not achieving is that once my form is modified it does not return to the value it previously had in the parameter, which does not happen in the live_component code that I just shared.
The only thing that I have different so to speak is that in the live_view I don’t use update and I can’t find a way to include something similar.