Skip to content

Commit a8b7981

Browse files
committed
fix(pat-validation): Fix problem with multiple form validation runs.
Due to some event listers calling each other multiple times, the form was validated up to 5 times in one validation run. This commit fixes multiple validation runs when a form element was disabled, e.g. when the submit button was disabled after validation errors.
1 parent 831ee60 commit a8b7981

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/pat/validation/validation.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ export default Base.extend({
6060
const debouncer = utils.debounce((e) => {
6161
logger.debug("Checking input for event", input, e);
6262
this.check_input({ input: input, event: e });
63-
if (this.disabled_elements.some((it) => it.disabled)) {
64-
// If there are already any disabled elements, do a check
65-
// for the whole form.
66-
// This is necessary otherwise the submit button is already
67-
// disabled and no other errors would be shown.
68-
// This is debounced, so it should not disturb too much while typing.
69-
for (const _input of this.inputs.filter((it) => it !== input)) {
70-
this.check_input({ input: _input });
71-
}
72-
}
7363
}, this.options.delay);
7464

7565
events.add_event_listener(
@@ -403,12 +393,26 @@ export default Base.extend({
403393
}
404394
input[KEY_ERROR_EL] = error_node;
405395

396+
let did_disable = false;
406397
for (const it of this.disabled_elements) {
407398
if (!it.disabled) {
399+
did_disable = true;
408400
it.setAttribute("disabled", "disabled");
409401
it.classList.add("disabled");
410402
logger.debug("Disable element", it);
411403
}
412404
}
405+
406+
// Do an initial check of the whole form when a form element (e.g. the
407+
// submit button) was disabled. We want to show the user all possible
408+
// errors at once and after the submit button is disabled there is no
409+
// way to check the whole form at once. ... well we also do not want to
410+
// check the whole form when one input was changed....
411+
if (did_disable) {
412+
logger.debug("Checking whole form after element was disabled.");
413+
for (const _input of this.inputs.filter((it) => it !== input)) {
414+
this.check_input({ input: _input, stop: true });
415+
}
416+
}
413417
},
414418
});

0 commit comments

Comments
 (0)