is invalid"-6ren"> is invalid"-我有这个模板 Insurance Expiry : 和迁移有, def change do create table(:buses) do # snipped add-6ren">
gpt4 book ai didi

elixir - 表单中的 date_select 失败, " is invalid"

转载 作者:行者123 更新时间:2023-12-04 11:36:21 24 4
gpt4 key购买 nike

我有这个模板

<div class="form-group">
<label>Insurance Expiry : </label>
<%= date_select f, :insurance_expiry, class: "form-control" %>
</div>

和迁移有,
def change do
create table(:buses) do

# snipped

add :insurance_expiry, :date, default: Ecto.Date.local

end

在 db 模型中,
 schema "buses" do

# snipped

field :insurance_expiry, :date

end

关于创建操作的调试信息,
[info] Processing by BusMan.BusController.create/2
Parameters: %{"_csrf_token" => "PDkIZycHTRJsEzwOEBJRXxo6MVIFJgAAHla/FI4Y5PQxTYdk/XakNg==", "_utf8" => "✓", "bus" => %{"bus_no" => "138", "chassis_no" => "nTHSNTH", "engine_no" => "RCHR989", "insurance_expiry" => %{"day" => "1", "month" => "10", "year" => "2019"}, "models" => "NTHRCG898", "reg_no" => "TN21W0613", "year_of_registration" => "1990"}, "format" => "html"}

表单提交失败:
Oops, something went wrong! Please check the errors below:

Insurance expiry is invalid

我只想输入一个日期,是 date_select是我需要的还是我缺少其他东西?

最佳答案

José Valim更正,使用 Ecto.Date而不是 :date正确解决了问题,并且不需要显式转换。

看起来传递给 create 函数的日期是一个映射。在插入之前,请尝试将其转换为日期
Ecto.Date.cast(%{"day" => "1", "month" => "10", "year" => "2019"})
我可以想象代码看起来像

selected_date = %{"day" => "1", "month" => "10", "year" => "2019"}
|> Ecto.Date.cast
|> Repo.insert!

关于elixir - 表单中的 date_select 失败, "<field> is invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com