[英]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.我尝试了几种不同的方法来解决这个问题,主要涉及支持表单的数据结构的变化。
Phoenix.HTML.FormData
protocol, and Phoenix only implements this for Plug.Conn
and Atom
Phoenix.HTML.FormData
协议的结构,而 Phoenix 只为Plug.Conn
和Atom
实现了这个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.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.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.