如何清除Phoenix LiveView表单中的输入?

问题描述 投票:0回答:1

我有一个不带数据层支持的表单的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>

我无法使用变更集支持表单,因为我没有使用Ecto。提交表单后,可以很好地处理提交,但是不会清除表单textarea。如何不借助Java脚本清除输入内容?

如果没有Java脚本就无法执行,如何[[with Javascript,但是without绕过LiveView phx-submit机制呢?

一些其他故障排除信息:

这里是我的事件处理程序:

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

我试图在返回元组中传递一个空的映射,就像这样:

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

我也尝试过定义post struct并传递它:

defmodule Post do defstruct: [:message] end

{:noreply, assign(socket, post: %Post{})}
elixir phoenix-framework phoenix-live-view
1个回答
1
投票
[正如Aleksei在他的评论中所说:您必须将新的Post结构从控制器传递给视图。例如这样:

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

© www.soinside.com 2019 - 2024. All rights reserved.