简体   繁体   English

Phoenix LiveView 表单提交,清除/重置输入值

[英]Phoenix LiveView form submit, clear/reset input value

I have created this simple form which I handle in my LiveView component.我创建了我在 LiveView 组件中处理的这个简单表单。 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?这是否总是通过 Ecto.Changeset,即使没有模式直接支持表单?

  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. LiveView 应该在提交后自动重置输入字段,但这可能是 Ecto 唯一的事情。 You can also write a JS hook for this if you want to clear the input.如果你想清除输入,你也可以为此编写一个 JS 钩子。 It's pretty simple.这很简单。

You can always redirect the user somewhere with a flash message for better UX.您始终可以使用 flash 消息将用户重定向到某个地方以获得更好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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