简体   繁体   中英

Phoenix LiveView form submit, clear/reset input value

I have created this simple form which I handle in my LiveView component. What is the communities best practice for clearing / resetting my form after I submit this?

I do want to take validations etc. into account. Is this then always via an Ecto.Changeset, even when no schema is backing a form directly?

  def handle_event("add", %{"text" => text}, socket) do
    IO.inspect(text)
    {:noreply, socket}
  end

  def render(assigns) do
    ~H"""
    <form phx-submit="add">
      <input type="text" name="text" placeholder="What needs to be done?" autofocus>
      <input type="submit" />
    </form>
    """
  end

LiveView should automatically reset the input field once it is submitted, but that might be an Ecto only thing. You can also write a JS hook for this if you want to clear the input. It's pretty simple.

You can always redirect the user somewhere with a flash message for better UX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM