Set Exceptions from Choice Field

Use dates from a choice-based field as exceptions in a GP Limit Dates-enabled Date field. Works with static choices, dynamically populated choices via GP Populate Anything.

Instructions

  1. Install this snippet with our free Code Chest plugin. https://gravitywiz.com/gravity-forms-code-chest/

  2. Add a choice-based field with date values as choices. Each choice value must match the format specified in the dateFormat variable below (e.g. if dateFormat is ‘yyyy-mm-dd’, choices should look like 2026-03-15).

  3. Update the configuration variables below.

Code

Filename: gpld-set-exceptions-from-choice-fields.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * Gravity Perks // Limit Dates // Set Exceptions from Choice Field
 * https://gravitywiz.com/documentation/gravity-forms-limit-dates/
 *
 * Use dates from a choice-based field as exceptions in a GP Limit Dates-enabled Date field.
 * Works with static choices, dynamically populated choices via GP Populate Anything.
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Code Chest plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 *
 * 2. Add a choice-based field with date values as choices. Each choice value must match
 *    the format specified in the `dateFormat` variable below (e.g. if `dateFormat` is
 *    'yyyy-mm-dd', choices should look like 2026-03-15).
 *
 * 3. Update the configuration variables below.
 */
var dateFieldId   = 3; // Update "3" to your Date field ID.
var sourceFieldId = 5; // Update "5" to your Choice field ID with date values.
var exceptionMode = 'disable'; // 'disable' = block these dates, 'enable' = only allow these dates, 'default' = invert availability.
var selectedOnly  = false; // Set to true to only use selected/checked choices as exceptions.
var dateFormat    = 'yyyy-mm-dd'; // Update to your date format (e.g. 'mm/dd/yyyy', 'dd.mm.yyyy', 'yyyy-mm-dd')

if ( ! window._gwGpldEfcInit ) {
	window._gwGpldEfcInit = true;

	gform.addFilter( 'gpld_datepicker_data', function( data, formId ) {
		if ( parseInt( formId, 10 ) !== GFFORMID || ! data || ! data[ dateFieldId ] ) {
			return data;
		}
		gwApplyExceptions( data[ dateFieldId ] );
		return data;
	} );

	jQuery( document ).on( 'gppa_updated_batch_fields', function( e, formId, updatedFieldIds ) {
		if ( parseInt( formId, 10 ) === GFFORMID && jQuery.inArray( String( sourceFieldId ), updatedFieldIds ) !== -1 ) {
			gwRefreshDatepicker();
		}
	} );

	jQuery( document ).on( 'change', '#field_' + GFFORMID + '_' + sourceFieldId + ' select, #field_' + GFFORMID + '_' + sourceFieldId + ' input', gwRefreshDatepicker );
}

gwRefreshDatepicker();

function gwApplyExceptions( fieldData ) {
	fieldData.exceptions    = gwGetChoiceDates();
	fieldData.exceptionMode = exceptionMode;
	fieldData.disableAll    = exceptionMode === 'enable';
}

function gwRefreshDatepicker() {
	var $input = jQuery( '#input_' + GFFORMID + '_' + dateFieldId );
	if ( $input.length && $input.hasClass( 'hasDatepicker' ) ) {
		$input.removeClass( 'hasDatepicker' );
		gformInitSingleDatepicker( $input );
	}

	var data    = window[ 'GPLimitDatesData' + GFFORMID ];
	var $inline = jQuery( '#datepicker_' + GFFORMID + '_' + dateFieldId );
	if ( data && data[ dateFieldId ] && $inline.length && $inline.hasClass( 'hasDatepicker' ) ) {
		gwApplyExceptions( data[ dateFieldId ] );
		$inline.datepicker( 'option', 'beforeShowDay', function( date ) {
			return GPLimitDates.isDateShown( date, data, dateFieldId );
		} );
	}
}

function gwGetChoiceDates() {
	var $field = jQuery( '#field_' + GFFORMID + '_' + sourceFieldId );
	var sel    = selectedOnly
		? 'select option:selected, input:checked'
		: 'select option, input[type="radio"], input[type="checkbox"]';
	var dates = [];

	$field.find( sel ).each( function() {
		var mdy = gwToMdy( jQuery( this ).val() );
		if ( mdy && dates.indexOf( mdy ) === -1 ) {
			dates.push( mdy );
		}
	} );

	return dates;
}

function gwToMdy( value ) {
	var valStr = String( value || '' ).trim();
	var fmt = String( typeof dateFormat !== 'undefined' ? dateFormat : 'yyyy-mm-dd' ).toLowerCase();

	var sep = fmt.replace( /[dmy]/g, '' )[0];
	if ( ! sep ) return null;

	var tokens = fmt.split( sep );
	var pos = {};
	for ( var i = 0; i < 3; i++ ) {
		pos[ tokens[i] ] = i;
	}

	var parts = valStr.match( /\d+/g );
	if ( ! parts || parts.length < 3 ) return null;

	var d = parts[ pos.dd ], m = parts[ pos.mm ], y = parts[ pos.yyyy ];
	
	if ( ! d || ! m || ! y || String( y ).length !== 4 ) return null;

	var p = function( n ) { return ( '0' + n ).slice( -2 ); };
	return p( m ) + '/' + p( d ) + '/' + y;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.