Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value for date selector in phoenix framework to current date

In the app I'm developing I have a date selector which will mostly be used with the current date as value (or a date a few days later). In order to reduce work for my users, I want to set today's date as default value.

I can easily set the year with: date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], but have not managed to do something similar for the month and day.

I've tried changing the code to date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], month: [default: 2], date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], month: [default: "2"], date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], month: [default: "02"], but none of these worked. Replacing default with value made no difference. What mistake am I making?

Thanks in advance

like image 432
Theemuts Avatar asked Jan 09 '23 07:01

Theemuts


2 Answers

Per the docs, the default option is given directly to date_select. So this should work:

date_select f, :date, default: {@current_year, 2, 13}

You can pass the date using other formats too, like a map instead of a tuple. Check datetime_select docs for the accepted formats.

like image 129
José Valim Avatar answered Jan 10 '23 22:01

José Valim


Just in case if anyone is wondering you can also set the default time by doing this

<%= datetime_select f, :start, class: "form-control", 
default: {{@current_year, @current_month, @current_day}, 
{@current_hour, @current_minute, @current_second}} %>
like image 23
Tommy Avatar answered Jan 10 '23 21:01

Tommy