Site icon Hip-Hop Website Design and Development

Undefined fieldId in gform.addFilter for limiting dates in datepicker – Gravity Forms

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.