Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 date_field, min and max year?

I am trying to set a maximum year value for my form using the rails 4 method ''date_field'' Because it seems to be possible for the user to type years bigger than 4 digits.

I've been trying to use it like this, but it does not seem to have any effect.

<%= f.date_field :date, style: "height: 30px", min: Time.now.year , max:         Time.now.year + 84 %>
like image 343
queroga_vqz Avatar asked Feb 13 '23 08:02

queroga_vqz


1 Answers

This is how I did it:

<%= f.date_field :date, min: Date.new(1994, 07, 03), max: Date.today %>

I limited the mininmum and max date the user could put using the built-in picker with the methods in Ruby's Date class. The only problem is that this isn't dynamic. And regarding your question of validating the year, I believe browsers do it automatically if it detects the input field with a date type (e.g <input id="user_date" min="1994-07-03" name="user[birthdate]" type="date">) even before the user submits the form. Hope this helped.

Edit: Updated Date.now to Date.new.

like image 154
x44x45x41x4E Avatar answered Feb 15 '23 10:02

x44x45x41x4E