Home > front end >  Rails date_field with min and max but no default value
Rails date_field with min and max but no default value

Time:07-22

I would like a form date_field with min and max limits, but with no default value. Currently when I set min and max values, they are resulting in the yyyy of the date_field being constrained and set to a default value on page load.

Here is the rails view:

<%= f.date_field("start_date", min: @min_days, max: @today_date) %>

Here is the resulting html:

<input min="2022-06-19" max="2022-07-20" type="date" name="start_date" id="start_date">

Since the min and max are both within the same year, the date_field form loads with the year '2022' populated and not editable:

enter image description here

I am not able to find a way to have the date_field form load with all blank default values while still constraining the user to the provided min and max window when they use the date selector calendar widget:

enter image description here

The reason for this desired UI behavior is so the user can better understand that the date_field is a blank and optional form input.

Thanks

CodePudding user response:

You cannot change this, it's nothing to do with Rails, it's part of the underlying HTML spec (and browser implementation).

Only way around it would be to use your own date widget.

FWIW, in my experience users aren't generally confused by this, as long as you have an obvious visual difference between optional and mandatory fields.

  • Related