Form doesn't always get cleared after submitting

When submitting the form, the inputs should clear once I pass an empty changeset, they dont seem to be doing that if I am successful on the first try.
Heres what I mean

  1. the first time the form was correct, it doesnt get cleaned (unexpected)
  2. then I failed with bad inputs, it doesnt get cleaned (expected)
  3. then I submit a correct version, it does get cleaned (expected)
2 Likes

heres a snippet of what I am using, is this expected? Am I missing somthing? Thanks in advance guys!

def handle_event("save", %{"volunteer" => volunteer_params}, socket) do
    case Volunteers.create_volunteer(volunteer_params) do
      {:ok, volunteer} ->
        socket =
          update(
            socket,
            :volunteers,
            fn volunteers -> [volunteer | volunteers] end
          )

        changeset = Volunteers.change_volunteer(%Volunteer{})

        {:noreply, assign(socket, :form, to_form(changeset))}

      {:error, changeset} ->
        {:noreply, assign(socket, :form, to_form(changeset))}
    end
  end

This is a somewhat unintuitive issue. The problem is that the initial value of the form assign is a new Volunteer, which is also the value of the assign after a successful submit. Of course, this makes sense when you want to clear the form to allow another submit with new data. But since the assign never changes to anything else, LiveView does not know that it should rerender things that depend on the assign.

When you attempt an unsuccessful submit, the assign gets updated with the invalid changeset, which fixes the problem. The reason why forms generated with phx.gen work is that they react to phx-change, which updates the assign.

Hopefully that makes sense, I’ve encountered this problem just a few days ago and came to this conclusion.

1 Like

Yep, that’s correct. Since LiveView stores its state on the server, you have to continuously update the server state as the form changes.

So in this case the only solution is to create and handle a phx-update?

You mean phx-change? phx-update is not an event, it’s an instruction on how to handle DOM patching. But yes, you must handle phx-change to keep track of your form’s state server-side. You could implement some JS to do it client-side, but this would defeat the purpose of LiveView.

Yes! Sorry I meant phx-change, its just to me its so weird to be forced to handle constant changes in order for the form to clear, but I understand, the diffing that LiveView does, doesn’t see a difference so thats why it doesn’t re-render it, thanks!

You can also update another assign with an id for the form. Changing the form id makes the form reset. That way you would also add signal to assigns for „this is now meant to be reset“.

Oh ya that’s a good idea! You don’t get validations then though it doesn’t sounds like OP needs them.

Perfect way to describe it, thanks!

While testing a new feature we ran into this situation which, at the time, we could not explain. Why was the form not updating?! :confused:

It turned out that the particular form input also had phx-debounce, to avoid flooding the server with phx-change events, and if no such events were sent before submit, then the form state from the server’s perspective never changed, and the form input was not re-rendered and cleared!

Your account of the problem was key to realizing what was going on :purple_heart:

2 Likes

What is actually going on here is that LiveView’s declarative abstraction is fundamentally leaking its imperative implementation. This is the sort of bug that is not supposed to happen with a declarative programming model.

In e.g. React this is fixed with controlled inputs which always have their state forcibly reset in lock-step with the render (this is declarative programming). Unfortunately this is one of very few things that I think genuinely cannot be fixed in LiveView because it is a consequence of server latency. In almost every case server latency is not a problem, but here it actually is.

This also means that @bartblast should take note, as if he implements controlled inputs correctly this is a case where Hologram will just be strictly and unavoidably better.

2 Likes

Yeah, this is already solved in Hologram since v0.6.0 :slight_smile: It’s called “synchronized inputs” and it works in a similar lock-step fashion - the component state is always the single source of truth, and input values are kept in sync declaratively through unidirectional data flow.

Check out the docs here: https://hologram.page/docs/forms

Forms: :white_check_mark: solved!

1 Like

You’re always spot on with your insights @garrison, thanks for chiming in!


I would like to offer a hint for others (and myself in the future) with a possible path forward. First, summarizing the issue:

Issue summary

Server state didn’t change?

  1. A form has id, phx-change, phx-submit and phx-debounce. The latter makes it more likely that a submit event may reach the server before any change event, but I believe is not fundamental to trigger the issue.
  2. The initial form state is something like to_form(%{}).
  3. The user submits the form, say with some text input with value "hello".
  4. The server handles the submit event and responds with what is intended to be an empty form, again to_form(%{}).
  5. From the server’s perspective, there was no change, so no reason to re-render the text input.
  6. The text hello remains visible :bug: :beetle: :cricket:

So in previous messages in this thread we talked about the “no change from the server’s perspective”. There’s a little bit more into play here, for which I refer to the LiveView documentation:

LiveView JS client specifics


The JavaScript client is always the source of truth for current input values. For any given input with focus, LiveView will never overwrite the input’s current value, even if it deviates from the server’s rendered updates.

So even if in step 4 above we’d assign to_form(%{content: "some other value"}), LiveView would still not update the visible input value (assuming it would be focused when submitting the form).

Possible solution

After evaluating a few options, I decided to go with a “pessimistic UI update” in my particular case. It means I want the client to clear the input only after the event is handled in the server and there were no validation errors, etc.

Eventually, the change is small but combines different concepts, so I decided to share.

  • push_event/3 to send (JS) events from server to client, only in the success path.
  • Global JS event listener to set DOM properties (unlike the JS.set_attribute/3 command that works on HTML attributes).
defmodule MyApp.MyLive
  def render(assigns) do
    ~H"""
    <.form
      for={@form}
      id="my-form"
      phx-change="validate"
      phx-submit="send"
      phx-debounce
      class="phx-submit-loading:opacity-50"
    >
      ...
    </.form>
    """
  end

  # ...

  def handle_event("send", %{"message" => message}, socket) do
    socket =
      case send(message) do
        {:ok, message} ->
          socket
          |> assign(form: to_form(%{}, as: :message))
          |> clear_input()

        {:error, changeset} ->
          socket
          |> assign(form: to_form(changeset, action: :submit))
      end

    {:noreply, socket}
  end

  defp clear_input(socket) do
    socket
    |> push_event("myapp:setproperty", %{
      "selector" => "##{socket.assigns.form[:content].id}",
      "property" => "value",
      "value" => ""
    })
  end
end
// app.js

// Similar to `JS.set_attribute/3` but sets a DOM property instead of an HTML
// attribute. The distinction is important for certain properties like `value`
// on input elements, which do not reflect changes made to the attribute.
// https://elixirforum.com/t/using-liveview-js-to-manipulate-input-values/46371/2?u=rhcarvalho
// https://hexdocs.pm/phoenix_live_view/syncing-changes.html#the-problem-in-a-nutshell
// https://hexdocs.pm/phoenix_live_view/form-bindings.html#javascript-client-specifics
window.addEventListener("phx:myapp:setproperty", event => {
  const target = document.querySelector(event.detail.selector);
  target[event.detail.property] = event.detail.value;
})

Outro

Other possible directions

  • “Optimistic” UI updates with JS commands, like

    phx-submit={
      JS.push("send")
      |> JS.dispatch(
        "setproperty",
        to: "#field-id",
        detail: %{property: "value", value: ""}
      )
    }
    

    This also works, instantly clears the text input (assuming an event handler for setproperty), and correctly recovers the submitted value in case of a validation error.

  • Client hooks with phx-hook, possibly using the new ColocatedHook in LV 1.1.

  • Listening for the phx:page-loading-stop event as part of the form submit flow.

  • Custom submit handler.

More on the LiveView docs

Quoting Form bindings — Phoenix LiveView v1.1.14, emphasis mine:

The JavaScript client is always the source of truth for current input values. For any given input with focus, LiveView will never overwrite the input’s current value, even if it deviates from the server’s rendered updates. This works well for updates where major side effects are not expected, such as form validation errors, or additive UX around the user’s input values as they fill out a form.

I continued reading the page looking for “conversely, what do I do when major side effects ARE expected?” – not found :slight_smile:

If anyone wants to continue the discussion in this direction we might be able to fill in that gap in the docs together!

Thanks! :purple_heart:

1 Like

Concurrent changes to a shared input value between the client and the server under latency are not trivially handled. You run into all the problems of distributed computing there. Even client side libraries in the past got this stuff wrong - and they didn‘t have a network on the path. LV does.

2 Likes