Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author ovann86

    (@ovann86)

    Hey,

    I should be able to add a filter inside the existing .datepicker() so you (and others) can add their own configuration.

    I’ll have a look at this later tonight.

    Thread Starter twesty

    (@twesty)

    Wow thanks for the rapid reply and really appreciate you helping me with this.

    Plugin Author ovann86

    (@ovann86)

    Hey,

    I hadn’t seen that Gravity Forms documentation – very good and note worthy for this plugin !

    Now I had a look at implementing it, and was able to remove weekends using the code below.

    Could you try adding it to your themes functions.php file (or where ever your keeping your custom code). (note this is an anonymous function for simplicity, that’s why it may look a little different to what you usually work with.

    If it doesnt work, check the page source code to check if it’s running and appearing in the source code. Then try changing the priority (the 10) – try lower, such as 1 then higher, such as 100.

    add_action('wp_footer', function () {
    	?>
    	<script>
    	gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    		optionsObj.firstDay = 1;
    		optionsObj.beforeShowDay = jQuery.datepicker.noWeekends;
    		return optionsObj;
    	});
    	</script>
    	<?php
    	},10);

    I’ve ran this code on my demo site, so you can see it here (for the time being, I’ll take it away in a few days time).

    https://demo.itsupportguides.com/gravity-forms-list-field-date-picker/single-page-form/

    Thread Starter twesty

    (@twesty)

    Works perfectly – thanks!

    I wasn’t able to get this to work despite the latest version of the plugin (1.4.1) and the latest gravity forms (1.9.15).

    I am wanting to limit the years shown in the list’s date pickers.

    For my normal date picker this works (in a custom javascript file):
    $(‘#input_55_19’ ).datepicker({ maxDate: ‘-17y’, changeMonth: true, changeYear: true });

    I want the exact same options/limits applied to my list field’s date pickers added by your plugin.

    Thanks!

    Plugin Author ovann86

    (@ovann86)

    chembro303-

    My understanding is you need to use the gform_datepicker_options_pre_init filter to customise the datepicker – that’s at least how I approach customisations. And you need to make sure where ever you include it is available before the datepicker is initialised, which is why I prefer to dump customisations in the page footer.

    If you were to do this as a WordPress function, adding it to the page footer you could use this code (sets the year range from now until 17 years).

    add_action('wp_footer', function () {
    	?>
    	<script>
    	gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    		optionsObj.minDate= '0y';
    		optionsObj.maxDate= '17y';
    		optionsObj.changeMonth= true;
    		optionsObj.changeYear= true
    		return optionsObj;
    	});
    	</script>
    	<?php
    	},10);

    You could also just try the JS in your external file. I would be interested in knowing how that works out – if it happens in time before the datepicker is initialised.

    E.g. your external file would need

    gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    		optionsObj.minDate= '0y';
    		optionsObj.maxDate= '17y';
    		optionsObj.changeMonth= true;
    		optionsObj.changeYear= true
    		return optionsObj;
    	});

    If you want to restrict the code to certain forms or files you can use the formId and fieldId parameters, as shown in the Gravity Forms documentation.

    e.g.

    if ( formId == 12 && fieldId == 1 ) {
            ... code ...
        }
        return optionsObj;

    Let me know how you go.

    You can see an example of the code in action on the demo website (I’ll leave it up there for a few days).

    https://demo.itsupportguides.com/gravity-forms-list-field-date-picker/single-page-form/

    Thanks so much for the extra response.

    When I use your provided “anonymous” function in functions.php, limited to my specific form, it successfully affects a normal “date” field in that form. BUT it doesn’t have any effect on the date field that’s in my list.

    As a side note, priority 10 didn’t work for me, but priority 50 and 100 both did.

    Here is the code in my functions.php:

    add_action('wp_footer', function () {
    	?>
    	<script>
    	gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    		if ( formId == 55 ) {
    			optionsObj.maxDate= '-17y';
    			optionsObj.changeMonth= true;
    			optionsObj.changeYear= true;
    		}
    		return optionsObj;
    	});
    	</script>
    	<?php
    },50);

    The above code limits the built-in gravity forms date field on the page to years 1999 and before (it’s for a D.O.B. field and they have to be 17+ yrs). But it has no effect on the D.O.B. field that’s part of a list field in that form.

    If I comment out the
    if ( formId == 55 ) {
    and its closing bracket, then both date fields in the form are correctly limited.

    Any idea what gives? The form is form 55, so I can’t figure out why it’s not working. It also had no effect to attempt to specify both field and form id instead of just form e.g.
    if ( formId == 55 && fieldId == 9 ) {

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘noweekend or other datepicker jquery tweeks’ is closed to new replies.