I have this template,
<div class="form-group">
<label>Insurance Expiry : </label>
<%= date_select f, :insurance_expiry, class: "form-control" %>
</div>
and migration has,
def change do
create table(:buses) do
# snipped
add :insurance_expiry, :date, default: Ecto.Date.local
end
and in db model,
schema "buses" do
# snipped
field :insurance_expiry, :date
end
Debug Info on create action,
[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"}
form submission fails with :
Oops, something went wrong! Please check the errors below:
Insurance expiry is invalid
I just want to input a date, Is date_select
is what I need or am I missing something else?
as José Valim corrected, using Ecto.Date
rather than :date
correctly solves the issue, and doesn't require explicit casting.
It looks like the date being passed to the create function is a map. Prior to inserting it, try casting it to date with
Ecto.Date.cast(%{"day" => "1", "month" => "10", "year" => "2019"})
I could imagine code that looked something like
selected_date = %{"day" => "1", "month" => "10", "year" => "2019"}
|> Ecto.Date.cast
|> Repo.insert!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With