Phoenix date_select with only Year

I want to use the phoenix date_select with only year. I managed to do this via builder as shown below.

<%= date_select f, :founding_date, builder: fn b -> %>
             <%= b.(:year,
              class: input_class,
              options: 1900..DateTime.utc_now().year
            ) %>
          <% end %>

However, in my schema I have defined the field like this,

field :statement_year, :date

So when only the year is there, ecto complains with the following error because month and day is missing

unrecognized date %{"year" => "2024"}

Is there a workaround for this?

If all you actually want is the year, I would consider just using an integer or string column. A year on its own is simply not a date, so the date column will not work. If you want to treat the year as meaning “The first of the year” then you could transform it into a full date by setting it to Jan 1.

1 Like