-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisibilityFormValidator.js
66 lines (58 loc) · 1.95 KB
/
VisibilityFormValidator.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
/**
* Avoid validating not visible inputs
*/
var VisibilityFormValidator = {};
VisibilityFormValidator.search = 'form:not([novalidate])';
VisibilityFormValidator.observer = null;
/**
* Initialized this validator for all actual and future forms
*/
VisibilityFormValidator.init = function() {
// Look for already available forms
$(VisibilityFormValidator.search).each(VisibilityFormValidator.prepareForm);
// Look for future added forms
VisibilityFormValidator.observe(function() {
// If one of the new elements is a form
$(this).filter(VisibilityFormValidator.search).each(VisibilityFormValidator.prepareForm);
// If one of the new elements contains a form
$(VisibilityFormValidator.search, this).each(VisibilityFormValidator.prepareForm);
});
};
/**
* Prepare a form to manually check its validity
*/
VisibilityFormValidator.prepareForm = function() {
$(this)
.attr('novalidate', true)
.on('submit', VisibilityFormValidator.validateForm);
};
/**
* Check form validity using browser native method
*/
VisibilityFormValidator.validateForm = function() {
var isValid = true;
// check only visible inputs
$('input:visible, select:visible,textarea:visible', this).each(function() {
// report validity returns validity of input and display error tooltip iff needed
isValid = isValid && this.reportValidity();
// break each loop if not valid
return isValid;
});
// do not submit if not valid
return isValid;
};
/**
* Observe body to look for new forms
* @param function cb The callback function
*/
VisibilityFormValidator.observe = function(cb) {
// Avoid multiple observers
if (VisibilityFormValidator.observer === null) {
VisibilityFormValidator.observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes && mutation.addedNodes.length > 0 && cb.call($(mutation.addedNodes));
});
}).observe(document.body, {childList: true, subtree: true});
}
}
VisibilityFormValidator.init();