-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgplcb-gppt-min-limit-soft-validation.js
77 lines (64 loc) · 1.97 KB
/
gplcb-gppt-min-limit-soft-validation.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
/**
* Gravity Perks // GP Limit Checkboxes // Add support for min limit Soft Validation with GP Page Transitions
*
* https://gravitywiz.com/documentation/gravity-forms-limit-checkboxes/
*
* Requires 1.3.12 or newer of GP Limit Checkboxes.
*
* Instructions:
* - Install using https://gravitywiz.com/gravity-forms-code-chest/
*/
window.gform.addFilter('gppt_validation_result', function (result, gppt, formId) {
var message = 'You must select at least {0} options.';
if (formId != GFFORMID) {
return result;
}
var gplc = window.GPLimitCheckboxes && window.GPLimitCheckboxes.instances && window.GPLimitCheckboxes.instances[formId];
if (!gplc) {
return result;
}
var validationMessage = '<div class="gfield_description validation_message">' + message + '</div>';
// Loops through groups and validate the ones where the fields are on the current page.
for (var i = 0; i < gplc.groups.length; i++) {
var group = gplc.groups[i];
var fields = group.fields;
var fieldsVisible = true;
var groupValidationMessage = validationMessage.gformFormat(group.min);
$(gplc.getSelector(group.fields)).each(function () {
var fieldVisible = $(this).is(':visible');
var pageVisible = $(this).closest('.gform_page').is('.swiper-slide-active');
if (!fieldVisible || !pageVisible) {
fieldsVisible = false;
return false;
}
});
if (!fieldsVisible) {
continue;
}
var belowMin = gplc.isGroupBelowMin(group);
var $parent = $(gplc.getSelector(group.fields)).parents('.gfield');
$parent.each(function () {
var $this = $(this);
if (belowMin) {
if (!$(this).hasClass(gppt.validationClass.split(' ')[0])) {
$this.addClass(gppt.validationClass);
$this
.children('.ginput_container')
.after(
groupValidationMessage
);
}
} else {
$this.removeClass(gppt.validationClass);
$this
.children('.ginput_container')
.next()
.remove();
}
});
if (belowMin) {
return false;
}
}
return result;
});