Skip to content

Commit 6607db3

Browse files
committed
Bug fix in toggle disabled that made the module incompatible with async validators
1 parent b82642d commit 6607db3

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/modules/toggleDisabled.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
if( !isCheckingIfFormValid ) {
5959
isCheckingIfFormValid = true;
6060
var $form = $(this).closest('form');
61-
if( valid && $form.isValid(conf.language, conf, false) ) {
61+
if(valid && peekIfFormIsSuccessfullyValidated($form, this, conf)) {
6262
toggleFormState($form, 'enabled');
6363
} else {
6464
toggleFormState($form, 'disabled');
@@ -83,11 +83,7 @@
8383
});
8484

8585
// Notice! Async validator can't be validated onkeyup
86-
// We can not determine if an input has an async validator by looking at the markup.
87-
// The solution here would be to add the flag `async` to addValidator configuration
88-
// for now we will have to hard code known async validators
89-
$formsToDisable.find('[data-validation~="server"],[data-validation~="dimension"]')
90-
.valAttr('event', 'change');
86+
$formsToDisable.find('[data-validation-async]').valAttr('event', 'change');
9187

9288
// Make all inputs validated on keyup, require validateOnEvent in validation config
9389
toggleFormState($formsToDisable, 'disabled');
@@ -101,4 +97,20 @@
10197
}
10298
});
10399

100+
// We want to peek at the form to check if all is valid, we don't want to trigger
101+
// the validators since that seems to cause unwanted side effects, that's hard to foresee
102+
function peekIfFormIsSuccessfullyValidated($form, excludeInputElement, config) {
103+
var allValid = true;
104+
$form.find('[data-validation]').each(function() {
105+
if (this !== excludeInputElement) {
106+
var $elem = $(this);
107+
if (!$elem.hasClass(config.successElementClass) || $elem.hasClass(config.errorElementClass)) {
108+
allValid = false;
109+
return false;
110+
}
111+
}
112+
});
113+
return allValid;
114+
}
115+
104116
})(jQuery, window);

0 commit comments

Comments
 (0)