首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Phoenix LiveView处理表单

如何使用Phoenix LiveView处理表单
EN

Stack Overflow用户
提问于 2019-04-17 18:54:23
回答 2查看 2.3K关注 0票数 2

我在凤凰城有一张表格,看起来像这样

代码语言:javascript
复制
<%= form_for @changeset, Routes.post_path(@conn, :create, @post), [method: "post", multipart: true], fn f -> %>
  <div class="row mt-3">
    <div class="form-group col-6">
      <%= input f, :title, "Title", [class: "form-control", type: "text", placeholder: "Enter Title", required: true] %>
    </div>
    <div class="form-group col-6">
     <%= input f, :description, "Description", [class: "form-control", type: "text", placeholder: "Enter Description", required: true] %>
    </div>
  </div>
  <div class="d-flex mt-3">
   <%= submit "Create Post" %>
  </div>
<% end %>

上面的表单运行良好,现在我想更改此表单以实现LiveView。所以我做了这样的事情

代码语言:javascript
复制
<%= form_for @changeset, "#", [method: "post", multipart: true], fn f -> %>
  <div class="row mt-3">
    <div class="form-group col-6">
      <%= input f, :title, "Title", [class: "form-control", type: "text", placeholder: "Enter Title", required: true] %>
    </div>
    <div class="form-group col-6">
     <%= input f, :description, "Description", [class: "form-control", type: "text", placeholder: "Enter Description", required: true] %>
    </div>
  </div>
  <div class="d-flex mt-3">
   <button phx-click="create-post" phx-value="form-value">Create Post"</button>
  </div>
<% end %>

我对form-value的位置感到困惑,需要发送什么才能在我的handle_event函数中获得包含titledescription的正确表单数据。

我尝试传递@changesetf,但他们发送的是包含我的titledescription的正确phx-value

不确定,如果我用LiveView正确地实现了表单,或者它需要以不同的方式完成。

EN

回答 2

Stack Overflow用户

发布于 2019-10-05 05:47:50

这里唯一的区别是您不想在form_for中使用匿名函数。

代码语言:javascript
复制
<%= form_for @changeset, "#", [phx_submit: "post", multipart: true], fn f -> %>
...
<% end %>

变成了

代码语言:javascript
复制
<%= f = form_for @changeset, "#", [phx_submit: "post", multipart: true] %>
...
</form>

不能对匿名函数内容进行区分。

票数 3
EN

Stack Overflow用户

发布于 2019-07-09 19:02:45

这是使用phoenix liveview实现表单的正确方法。

代码语言:javascript
复制
<%= form_for @changeset, "#", [phx_submit: "post", multipart: true], fn f -> %>
  <div class="row mt-3">
      <div class="form-group col-6">
        <%= input f, :title, "Title", [class: "form-control", type: "text", placeholder: "Enter Title", required: true] %>
      </div>
      <div class="form-group col-6">
       <%= input f, :description, "Description", [class: "form-control", type: "text", placeholder: "Enter Description", required: true] %>
      </div>
    </div>
    <div class="d-flex mt-3">
      <%= submit "Create Post", phx_disable_with: "Posting..." %>
    </div>
<% end %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55725901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档