Skip to content

Commit 55e3e9a

Browse files
authored
gpld-populate-new-minimum-date-into-linked-date-field.js: Fixed an issue with default value of field not getting used to apply minimum date.
1 parent 072a5c4 commit 55e3e9a

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

gp-limit-dates/gpld-populate-new-minimum-date-into-linked-date-field.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,44 @@
1010
* 1. Install this snippet with our free Custom JavaScript plugin.
1111
* https://gravitywiz.com/gravity-forms-code-chest/
1212
*/
13-
gform.addAction( 'gpld_after_set_min_date', function( $input, date ) {
14-
$input.datepicker( 'setDate', date );
13+
const sourceFieldId = 25; // Replace with the ID of the source field (Field A)
14+
15+
// Initialize field events on form render.
16+
document.addEventListener( 'gform/post_render', (event) => {
17+
const formId = event.detail.formId;
18+
const $field = getSourceField(formId, sourceFieldId);
19+
triggerFieldEventsIfValueExists($field);
20+
});
21+
22+
// Handle conditional logic changes.
23+
gform.addAction(
24+
'gform_post_conditional_logic_field_action',
25+
(formId, action, targetId, defaultValues, isInit) => {
26+
// Only for fields and when we are not hiding them.
27+
if (action == 'hide' || targetId == '#gform_submit_button_' + formId) {
28+
return;
29+
}
30+
31+
const $field = getSourceField(formId, sourceFieldId);
32+
triggerFieldEventsIfValueExists($field);
33+
}
34+
);
35+
36+
// Triggers input and change events on a field if it has a value.
37+
const triggerFieldEventsIfValueExists = ($field) => {
38+
const value = $field.val();
39+
if (value) {
40+
requestAnimationFrame(() => {
41+
$field.trigger('input').trigger('change');
42+
});
43+
}
44+
};
45+
46+
// Get the source field based on form ID and field ID.
47+
const getSourceField = (formId, fieldId) => {
48+
return jQuery(`#input_${formId}_${fieldId}`);
49+
};
50+
51+
gform.addAction( 'gpld_after_set_min_date', function( $input, date ) {
52+
$input.datepicker( 'setDate', date );
1553
} );

0 commit comments

Comments
 (0)