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 %>
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
.
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