0
votes

i am using rails date time select. with start year and end year options. and here a user should be able to select all dates from the last year to till the current date. a user should not be allowed to select future dates for the current year. how can i do this? if i menction start date and end date options will that restrict the dates for the previous year also?

<%= f.datetime_select :check_out_req, { start_year: Date.today.year - 1,:end_year => Date.today.year,ampm: true, order: [:day, :month, :year] }, { class: "form-control" }
1

1 Answers

2
votes

The <select>s that datetime_select generates are static, so they can't disable options depending on other selections. Eg, Rails won't disable Sept 2015 without Sept also being disabled for 2014.

You have a few options to get what you want:

  • Validate the date on your model. Check that it's within 1.year.ago.to_date..Date.today, and display a typical validation error to the user if it's not.
  • Implement some JavaScript on the frontend that enables and disables month and day selectors as year and month are selected.
  • Pick a JS plugin that will help you. There are lots around: