Quick tip: How to prevent past dates in Drupal Date module's Popup
I recently had to prevent a user from selecting past dates using a Date module date popup widget. Turns out that this is really simple to do in the jQuery UI widget, using the minDate option.
However, I couldn't find anything in the Date module's documentation that allowed me to pass extra options to the widget. Messing with global JavaScript variables to achieve this turned out to be messy until, low and behold, I found this commit to the Date module that actually now allows this:
// In a custom Form.$form['date_picker'] = array(
'#title' => t('Pick a date'),
'#type' => 'date_popup',
'#required' => TRUE,
'#date_format' => 'm/d/Y',
'#date_label_position' => 'within',
'#date_year_range' => '-0:+3',
'#datepicker_options' => array(
// Look!
'minDate' => 0,
),);// To add this to existing widgets in entity forms it's a matter of form_altering it and adding that extra #datepicker_options array.
Pretty sweet huh? This feature made it into version 7.x-2.6 but hasn't gotten much attention and the documentation hadn't been updated so I took a couple minutes to do it so others won't waste some time figuring this out... Hope this helped!
Tags:
30Jan2013
Language
English
Category:
Reviewed: Yes