0

I've added support for datepicker on date fields in entry detail edit admin page of Gravity Forms. I did this by simply enqueuing the gform_datepicker_init script in admin_enqueue_scripts hook. This works perfectly as datepicker is displayed for date fields which was not available by default in entry detail edit page.

My form has additional requirements and I want to limit the dates in a datepicker field. So, for that I want to take advantage of a JS filter gform_datepicker_options_pre_init provided by Gravity Forms. When I tried with following code, which is put into a hooked function for admin_enqueue_scripts, the fieldId is undefined as reported by console log and hence this doesn't work:

// Allow past and current dates only
$custom_datepicker_init = "
jQuery(document).ready(function($) {
    gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
        // Apply to field 101 of form 1 only
        if(formId == 1 && fieldId == 101) {
            optionsObj.maxDate = 0;
        }
        console.log('formId: ' + formId + ', fieldId: ' + fieldId); // <--- fieldId is undefined
        return optionsObj;
    } );
});";
wp_add_inline_script( 'gform_datepicker_init', $custom_datepicker_init, 'before' );

I tried putting this code into other relevant hooks too, but it didn't work.

Someone please guide me why fieldId is undefined here and if I need to put this code into some other hook to work, please suggest.

0

After debugging, I noticed that the formId in the hooked function for filter was actually fieldId for the date field and hence the fieldId undefined.

Here no actual formId is received in hooked JS filter function and the actual fieldId is passed as formId. So, quick workaround I applied using formId only, is:

// Allow past and current dates only
$custom_datepicker_init = "
jQuery(document).ready(function($) {
    gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
        // Apply to field 101 of form 1 only
        // As this is nested form, so reports fieldId as formId and
        // leaving fieldId undefined, behave accordingly :)
        if(formId == 101) {
            optionsObj.maxDate = 0;
        }
        return optionsObj;
    } );
});";
wp_add_inline_script( 'gform_datepicker_init', $custom_datepicker_init, 'before' );

I think, this behavior is due to use of Gravity Forms Nested Forms in current Gravity Form. Hope it helps someone while debugging the problem.

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.