datedrupal-7onselectmindate

drupal 7 date_popup module (update minDate onSelect event)


I'm using drupal 7 module date (date_popup). I have 2 datepickers. I need to update minDate on the second datepicker on "onSelect" event by click on the first one. It works but only for the second click. If I click on the first datepicker and select date nothing happens and minDate is not updated. When I select the second time it works well. Why doesn't it work from my first click?

Here is my datepicker init php code for my module.

    <?php
    $form['leave'] = array(
            '#type' => 'date_popup', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
            '#title' => t('Leave'),
            '#default_value' => date('Y-m-d'),
            '#date_format' => $format,
            '#required' => TRUE, // Added
            '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
            '#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
            '#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
            '#datepicker_options' => array(
                'changeMonth' => false,
                'changeYear' => false,
                'altField' => '#edit-leave-alt',
                'showOtherMonths' =>true,
                'selectOtherMonths' =>false,
                'altFormat' => 'D, M d, yy',
                'minDate' => 0,
            ), // Optional, as of 7.x-2.6+, used to pass in additional parameters from the jQuery Datepicker widget.
            '#attributes' => array('readonly' => 'readonly')

        );

$form['leave'] = array(
        '#type' => 'date_popup', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
        '#title' => t('Leave'),
        '#default_value' => date('Y-m-d'),
        '#date_format' => $format,
        '#required' => TRUE, // Added
        '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
        '#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
        '#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
        '#datepicker_options' => array(
            'changeMonth' => false,
            'changeYear' => false,
            'altField' => '#edit-leave-alt',
            'showOtherMonths' =>true,
            'selectOtherMonths' =>false,
            'altFormat' => 'D, M d, yy',
            'minDate' => 0,
        ), // Optional, as of 7.x-2.6+, used to pass in additional parameters from the jQuery Datepicker widget.
        '#attributes' => array('readonly' => 'readonly')

    );
    ?>

Here is my js code

(function($)  {
    Drupal.behaviors.ifly_search = {
        attach: function (context, settings) {
            $("#edit-leave-datepicker-popup-0").datepicker({
                onSelect: function(date,param) {
                    var data = new Date(param.currentYear, param.currentMonth, param.currentDay);
                    $("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
                },
            });
        }
    };
})(jQuery);

Solution

  • Just to flesh out my comment. Try this instead:

    (function($)  {
            Drupal.behaviors.ifly_search = {
            attach: function (context, settings) {
              $("#edit-return-datepicker-popup-0").datepicker({minDate: new Date()});
              $("#edit-leave-datepicker-popup-0").datepicker({
                onSelect: function(date,param) {
                    var data = new Date(param.selectedYear, param.selectedMonth, param.selectedDay);
                    $("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
                },
              });
            }
        };
    })(jQuery);