0

I am using a javascript datepicker to allow users to select a date (which I have functioning in my form). What I don't know how to do is limit the dates that can be selected. Is there a way to do this using the datepicker instead of validating the form on submission?

Here is the code I am using to create the date selector:

drupal_add_library('system', 'ui.datepicker');
drupal_add_js($path . '/providers/support/js/datepicker.js');
drupal_add_js(array('dateselector' => array('date_format' => $date_picker_format)), 'setting');
return array(
  '#type' => 'textfield',
  '#default_value' => $current_value,
  '#attributes' => array('class' => array('datefield')),
);

Am I missing an option somewhere?

0

To limit available dates you want minDate, maxDate and beforeShowDay datepicker options. beforeShowData is useful to limit allowed values programatically.

minDate and maxDate can be passed to js as:

drupal_add_js(array('dateselector' => array('date_format' => $date_picker_format, 'minDate' => '+1d')), 'setting');

.. and here's an example of beforeShowDay usage: https://drupal.stackexchange.com/a/60121/8437

btw, I'd recommend using Date's date_popup form element in conjunction with Date Restrictions, for a variety of advanced restrictions.

| improve this answer | |
  • Thanks! That really helped. The link provided helped as well. – user5013 Mar 3 '14 at 1:28
0

I don't see, how you initialize the datepicker (probably in */providers/support/js/datepicker.js file), but for this functionality you should use maxDate and minDate options of datepicker library.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.