简体   繁体   English

如何清除 Phoenix LiveView 表单中的文本区域?

[英]How do you clear a textarea in a Phoenix LiveView form?

I have a Phoenix LiveView with a form that is not backed by a data layer, like so:我有一个没有数据层支持的表单的 Phoenix LiveView,如下所示:

<%= f = form_for :post, "#", [phx_submit: :create_post %>
  <%= textarea f, :message, placeholder: "Say something:" %>
  <%= hidden_input f, :user_id, value: @current_user.account_id %>
  <%= submit "Post" %>
</form>

I can't back the form with a changeset because I am not using Ecto.我无法使用变更集支持表单,因为我没有使用 Ecto。 After submitting the form, the submission is processed just fine, but the form textarea is not cleared.提交表单后,提交处理就好了,但是表单textarea没有清空。 How can I clear inputs without resorting to Javascript?如何在不使用 Javascript 的情况下清除输入?

If I can't do it without Javascript, how can I do it with Javascript, but without bypassing the LiveView phx-submit mechanisms?如果没有 Javascript 我不能做到,我怎么能用Javascript 做到这一点,但又不绕过 LiveView phx-submit机制?

Some additional troubleshooting information:一些额外的故障排除信息:

Here is my event handler:这是我的事件处理程序:

def handle_event("create_post", %{"post" => post_params}, socket) do
  thread_id = socket.assigns.thread.id
  user_id = post_params["user_id"]
  posts = Forums.append_post!(thread_id, user_id, post_params)
  UdsWeb.Endpoint.broadcast_from(self(), build_topic(thread_id), "new_post", %{posts: posts})
  {:noreply, assign(socket, :posts, posts)}
end

I've tried several different approaches to fix the problem, mostly involving variations of data structures backing the form.我尝试了几种不同的方法来解决这个问题,主要涉及支持表单的数据结构的变化。

  • I've tried backing the form with a map.我试过用地图支持表单。 This doesn't work because forms must be backed with structures that implement the Phoenix.HTML.FormData protocol, and Phoenix only implements this for Plug.Conn and Atom这是行不通的,因为表单必须支持实现Phoenix.HTML.FormData协议的结构,而 Phoenix 只为Plug.ConnAtom实现了这个
  • I've tried using a struct, but this doesn't work for the same reason as maps我试过使用结构,但这与地图的原因不同
  • I don't have a Conn to use in my form, because this is a LiveView, but I could create a Conn in the LiveView controller, so I did.我的表单中没有可使用的Conn ,因为这是一个 LiveView,但我可以在 LiveView 控制器中创建一个Conn ,所以我做到了。 I backed the form with it and passed a new instance through in the event handler for post creation.我用它支持表单并在事件处理程序中传递一个新实例以进行后期创建。 This did not solve the problem.这并没有解决问题。
  • Finally, I changed the textarea to a text_input , and this input cleared immediately on submission of the form.最后,我将textarea更改为text_input ,并在提交表单时立即清除此输入。 So it seems the problem is specific to the textarea element.因此,问题似乎特定于textarea元素。 I'm not sure whether or not this is a bug with Phoenix.我不确定这是否是 Phoenix 的错误。

As Aleksei said in his comment: You have to pass a new Post struct from your controller to your view.正如 Aleksei 在他的评论中所说:您必须将一个新的Post结构从您的控制器传递到您的视图。 For example like this:例如像这样:

def handle_event("create_post", post, socket) do
    # Here do what you want with the data from the "post" parameter

    {:noreply, assign(socket, :post, %Post{})}   
end

Even it behaves like a SPA it is not a SPA, so you still need to use the backend router and redirect it back to the index page.即使它的行为类似于 SPA,它也不是 SPA,因此您仍然需要使用后端路由器并将其重定向回index页面。 Your form is on the index page but the resource is not the post's index page, it is post/new .您的表单在index页面上,但资源不是post's index页面,它是post/new

So, you need to use push_redirect (not redirect ):因此,您需要使用push_redirect (而不是redirect ):

|> push_redirect(to: UdsWeb.post_index_path(socket, :index))
def handle_event("create_post", %{"post" => post_params}, socket) do
  thread_id = socket.assigns.thread.id
  user_id = post_params["user_id"]
  posts = Forums.append_post!(thread_id, user_id, post_params)
  UdsWeb.Endpoint.broadcast_from(self(), build_topic(thread_id), "new_post", %{posts: posts})
  {:noreply,
   socket
   |> push_redirect(to: UdsWeb.post_index_path(socket, :index))}
end

Hex: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#push_redirect/2十六进制: https : //hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#push_redirect/2

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

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