Date_select on the form fails with "<field> is invalid"
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 the db model,
schema "buses" do # snipped field :insurance_expiry, :date end Debugging action creation information,
[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 failure:
Oops, something went wrong! Please check the errors below: Insurance expiry is invalid I just want to enter a date, Is date_select is what I need, or am I missing something else?
+7
1 answer
as Jose Valim fixed using Ecto.Date , and not :date , solves the problem correctly and does not require explicit casting.
It looks like the date passed to the create function is a map. Before embedding it, try performing it for today with
Ecto.Date.cast(%{"day" => "1", "month" => "10", "year" => "2019"})
I could imagine a code that looked like
selected_date = %{"day" => "1", "month" => "10", "year" => "2019"} |> Ecto.Date.cast |> Repo.insert! +8