axelson

axelson

Scenic Core Team

Trouble resetting a LiveView form programmatically

I’m trying to reset my LiveView form programmatically (on submit) but I’m having lots of trouble getting the form to reset programmatically in a way where my LiveView can trigger the reset.

My form is driven by an Ecto embedded schema that is specific to this form (since my persistence is not via Ecto itself but instead a remote api that is mediated by a separate js script). I am using core components which I would expect to support this.

Things I’ve tried/links I’ve tried:

No matter what I try I can’t get the form to change based on my liveview assigns unless I manually set value={@something}. But none of the examples I see include setting value and I don’t see a straightforward way to get the value of a field. I guess there’s Phoenix.HTML.Form.input_value but it feels wrong to use that directly in a form (it would make sense in a re-usable component but not in a form).

I’d like to avoid needing to trigger the reset from javascript if possible because this seems to me like something that should be possible directly from LiveView (and from other answers here it appears to be).

I feel like I’m missing something simple, hence the forum post.

My LiveView looks like:

  defmodule MyForm do
    use Ecto.Schema

    @primary_key false
    embedded_schema do
      field :note, :string
      field :amount, :string
    end
  end

  def reset_form(socket) do
    assign(socket,
      form:
        %MyForm{}
        |> Ecto.Changeset.change()
        |> Phoenix.Component.to_form()
    )
  end

  def render(assigns) do
    ~H"""
    <div class="w-full max-w-md mx-auto p-4">
      <div className="relative">
        <.form for={@form} phx-change="validate" phx-submit="submit">
          <.input
            id="note-input"
            field={@form[:note]}
            class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white shadow-sm transition-colors duration-200"
            type="text"
            placeholder="Note"
          />
          <.input
            id="amount-input"
            field={@form[:amount]}
            type="text"
            inputmode="decimal"
            placeholder="amount"
          />

          <.button>Send!</.button>
          <button type="reset" name="reset">Reset</button>
        </.form>
      </div>
    </div>
    """
  end

  def handle_event("validate", params, socket) do
    socket = reset_form(socket)
    {:noreply, socket}
  end

  def handle_event("submit", params, socket) do
    socket = reset_form(socket)
    {:noreply, socket}
  end

Note: the reset button does reset the form but I don’t think I can directly trigger the reset button via LiveView

Here’s a repo with a reproduction:
https://github.com/axelson/phoenix_live_view_reset_form_repro

Steps to reproduce

  • Run mix.setup
  • Run iex -S mix phx.server
  • Visit http://localhost:4000
  • Fill out the form
  • Press submit
  • The form is not cleared but I would expect it to be cleared because I call reset_form which assigns a fresh @form assign

Most Liked

LostKobrakai

LostKobrakai

The simplest way to reset a form is updating its id attribue. That makes LV consider the node to be a replacement for the old one, resetting any state it might still hold onto.

LostKobrakai

LostKobrakai

Last Post!

LostKobrakai

LostKobrakai

The only observable behaviour in the form would be deleting an existing row, because the rows are only tagged for deletion within the livecycle of the form/changeset modeling it.

ScreenFloat Bildschirmaufnahme von Firefox Developer Edition am 18_11_2024, 17_31_18(1)

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement