views:

422

answers:

2

I have a select_date that I want to restrict the months that a user can select.

<%= select_date(Time.now, :order => [:month, :day, :year ], :datetime_separator => '-', :time_separator => ':', :discard_seconds => true, :discard_minutes => true, :start_month => Time.now.month, :end_month => Time.now.month +1, :start_year => Time.now.year, :end_year => Time.now.year, :prefix => 'start_date') %>

Yet, there doesn't seem to be a way to do this. You can't restrict the number of months a select_date displays. Anyone have a way around this?

A: 

What about doing it in the validation? Or throw these date helpers away (who wants to input dates with them anyway, I mean...) and use a real calendar javascript thingy.

I can recommend http://www.dynarch.com/projects/calendar/old/.

I use the following snipped to automatically add them to my date input text fields. We use dd.mm.yyyy year style here, but changing it to something else shouldn't take too much time.

function setupDateFields() {
    $$("input.date").each(function(input) {
      setupDateField(input);
    });

}

function setupDateField(id, time) {
  var field = $(id);

  var img = new Element('img', {
          src: "/images/icons/calendar.png",
          alt: "date",
          id: field.id + "_img",
          "class": "dateIcon"});
  field.insert({after: img});

  var format = time ? "%Y.%m.%d %H:%M:%S" : "%d.%m.%Y";

  Calendar.setup({
      date           :    new Date(),
      ifFormat       :    format,
      showsTime      :    time,
      inputField     :    field.id,
      button         :    img.id,
      firstDay       :    1, /* sunday */
      others         :    true
  });
}

document.observe("dom:loaded", setupDateFields);
reto
A solution, but I don't want to have to re-implement everything once again.Anyone know how to modify the source code to add a :start_month, :end_month to the helpers?
Tyler K
I looked through the source and I fear you cant do it without copying parts of the date select helper library (no configuration options, and there's no start_month/end_month options). But perhaps somebody else has an idea. But look at it yourself, the code is pretty 'easy', its in the date_helper.rb file.
reto
Has anyone solved this using another helper? Possible datetime?Modifying the source code doesn't seem likely since it consists of three lines:def select_date(date = Date.current, options = {}, html_options = {}) DateTimeSelector.new(date, options, html_options).select_date end
Tyler K
A: 

There isn't a way, that I know of, to solve this using Rails.

Instead I used this:

http://code.google.com/p/calendardateselect/

restrict the date range as such:

<%= f.calendar_date_select :start_date, :embedded => true, :valid_date_check => "( (date.stripTime() < ((new Date()).stripTime()).setMonth(new Date().getMonth() + 1)) && ( date.stripTime().setDate(date.getDate() + 1) > (new Date()).stripTime()) )", :year_range => [Time.now.year, Time.now.year], :time => false %>

This allows me to limit the amount of time that a user can select by any range, but in my case by month.

Tyler K