Reputation: 526
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?
Upvotes: 0
Views: 2041
Reputation: 526
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.
Upvotes: 1
Reputation: 16732
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);
Upvotes: 0