【发布时间】:2017-01-03 13:48:52
【问题描述】:
这特别是我从元素中创建自定义日期。我有一个表格可以收集一个人的出生日期和月份(我对年份不感兴趣)。因此,我使用以下内容为本文所涉及的 day_and_month_of_birth 元素创建表单条目:
<div class="form-group">
<div class="row">
<div class="col-md-5">Day / Month of Birth</div>
<%= date_select f, :day_and_month_of_birth, builder: fn b -> %>
<div class="col-md-3">
<%= b.(:day, []) %>
</div>
/
<div class="col-md-4">
<%= b.(:month, []) %>
</div>
<% end %>
<%= error_tag f, :day_and_month_of_birth %>
</div>
</div>
在我的模型中,我有:
defmodule RocfDev.Registration do
use RocfDev.Web, :model
schema "registration" do
field :title, :string, virtual: true
field :firstname, :string, virtual: true
field :surname, :string, virtual: true
field :day_and_month_of_birth, Ecto.Date, virtual: true
end
def changeset(model, params \\ %{}) do
model
|> cast(params, [
:title,
:firstname,
:surname,
:day_and_month_of_birth
])
end
目前,当我发布表单时,我收到错误 unrecognized date %{"day" => "1", "month" => "1"}。
我想这是因为 registration 架构中没有 day 和 month 字段。我的问题是:
第一,由于我对出生年份不感兴趣,Ecto.Date 仍然适合用于day_and_month_of_birth 字段吗?
2nd,我该如何解决这个错误?
【问题讨论】:
标签: elixir phoenix-framework ecto