The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Multiple Date Picker issue

Labels

This Discussion is public

Notifications

Hi All,

Could use some help with multiple calendar date picker issues.

My form has options for including up to 4 dogs Date of birth. Each dogs date of birth is required and only dates in the past should be able to be chosen.

I have a date picker for choosing a dogs age and it will work using this code:

add_action('frm_date_field_js', 'limit_my_date_field1');

function limit_my_date_field1($field_id){

  if($field_id == 'field_fysda'){ //change FIELDKEY to the key of your date field

    echo ',minDate:-6570,maxDate:0';

  } else {

    echo ',minDate: null, maxDate: null';

  }

}

Further in the form I have two date pickers for “drop off” and “pick up”.

Drop off dates can only be in the future and pick up must be at least one day after drop off date.

These also work well with the following.

For drop off:

add_action('frm_date_field_js', 'limit_my_date_field');

function limit_my_date_field($field_id){

  if($field_id == 'field_1yb7k'){ //change FIELDKEY to the key of your date field

    echo ',minDate:0,maxDate:+365';

  } else {

    echo ',minDate: null, maxDate: null';

  }

}

For Pickup:

add_action('frm_date_field_js', 'start_and_end_dates', 10, 2);

function start_and_end_dates($field_id, $options){

$key_one = '1yb7k';// Change pickup to the KEY of the first date field

$key_two = 'ppt38';// Change dropoff to the KEY of the second date field

$days_between = 1;// Change 0 to the number of days that should be between the start and end dates

if ( $field_id == 'field_'. $key_one ) {

echo ',beforeShowDay: function(dateOne){var secondDate=$("#field_' . $key_two . '").datepicker("getDate");if(secondDate==null){return[true];}var modDate=new Date(secondDate);modDate.setDate(modDate.getDate()-' . $days_between . '+1);return [(dateOne < modDate)];}';

} else if ( $field_id == 'field_' . $key_two ) {

echo ',beforeShowDay: function(dateTwo){var firstDate=$("#field_' . $key_one . '").datepicker("getDate");if(firstDate==null){return[true];}var modDate=new Date(firstDate);modDate.setDate(modDate.getDate() +  ' . $days_between . '-1);return [(dateTwo > modDate)];}';

} else {

    echo ',beforeShowDay: null';

  }

}

My problem I have is that the “date of birth” and the “drop off” use the same code which I changed the “limit my date field” name as I seen suggested in the help section.

But if I use these two similar code snippets for either “date of birth” or “Drop off” and only the last piece of code placed will work the that specific date picker. The other date picker defaults and visitor can pick any date past or future..

I expect all these variations should be in one block of code rather than multiples but am not sure how to set it up properly. I am using snippets to add the php.

AND Yes I do not know much about PHP coding.

Any help would be appreciated.

Barr

Here's your code refactored into a single function:

add_action('frm_date_field_js', 'my_custom_date_options', 10, 2);
function my_custom_date_options($field_id, $options) {

$key_one = '1yb7k';
$key_two = 'ppt38';
$days_between = 1;

switch ($field_id) {

case 'field_fysda':
echo ',minDate:0,maxDate:+365';
break;

case 'field_1yb7k' :
echo ',minDate:0,maxDate:+365, beforeShowDay: function(dateOne){var secondDate=$("#field_' . $key_two . '").datepicker("getDate");if(secondDate==null){return[true];}var modDate=new Date(secondDate);modDate.setDate(modDate.getDate()-' . $days_between . '+1);return [(dateOne modDate)];}';
break;

default:
echo ',minDate: null, maxDate: null, beforeShowDay: null';
}

}

Thanks Victor,

I tested the code you provided but it does not seem to work. With your code the date picker's did not offer the calendar pop up at all for all date fields. Maybe I needed to add something to it?

I have reverted back to the original code (as per original above) for the "drop off" and "pick up field" and they work as required.  If I add in the code individually for the "Dogs Age" it will also work but the "drop off" field quits working. I guess one is over-riding the other.

The form is here: http://areak9.com/wp_areak9/guest_reservation_form/

I am not sure if there is a solution? Again my php coding is extremely limited.

thanks again for reaching out.

Barry

The problem with trying to refactor code through the forum is that I can't test it. The only way to test it is through access to the admin area. Sorry it didn't work for you. Good luck.

Discussion closed.