Skip to content

Commit a986b63

Browse files
author
Andrew Korshunov
committed
#97 Implemented delayed validation by introducing delayedValidation option
1 parent b8b9965 commit a986b63

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/index.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ export interface ValidationServiceOptions {
422422
watch: boolean;
423423
root: ParentNode;
424424
addNoValidate: boolean;
425+
delayedValidation: boolean
425426
}
426427

427428
/**
@@ -1068,6 +1069,15 @@ export class ValidationService {
10681069
const cb: ValidationEventCallback = async (event, callback) => {
10691070
let validate = this.validators[uid];
10701071
if (!validate) return true;
1072+
1073+
if (
1074+
this.options.delayedValidation &&
1075+
event && event.type === 'input' &&
1076+
!input.classList.contains(this.ValidationInputCssClassName)
1077+
) {
1078+
// When delayedValidation=true, "input" only takes it back to valid. "Change" can make it invalid.
1079+
return;
1080+
}
10711081

10721082
this.logger.log('Validating', { event });
10731083
try {
@@ -1089,9 +1099,19 @@ export class ValidationService {
10891099
}, this.debounce);
10901100
};
10911101

1092-
const validateEvent = input.dataset.valEvent || input instanceof HTMLSelectElement ? 'change' : 'input';
1093-
input.addEventListener(validateEvent, cb.debounced);
1094-
cb.remove = () => input.removeEventListener(validateEvent, cb.debounced);
1102+
const validateEvent = input.dataset.valEvent || input instanceof HTMLSelectElement ? 'change' :
1103+
(this.options.delayedValidation ? 'input change' : 'input');
1104+
const events = validateEvent.split(' ');
1105+
1106+
events.forEach((eventName) => {
1107+
input.addEventListener(eventName, cb.debounced);
1108+
});
1109+
1110+
cb.remove = () => {
1111+
events.forEach((eventName) => {
1112+
input.removeEventListener(eventName, cb.debounced);
1113+
});
1114+
};
10951115

10961116
this.inputEvents[uid] = cb;
10971117
}
@@ -1378,12 +1398,14 @@ export class ValidationService {
13781398
root: document.body,
13791399
watch: false,
13801400
addNoValidate: true,
1401+
delayedValidation: false,
13811402
}
13821403

13831404
/**
13841405
* Load default validation providers and scans the entire document when ready.
13851406
* @param options.watch If set to true, a MutationObserver will be used to continuously watch for new elements that provide validation directives.
13861407
* @param options.addNoValidate If set to true (the default), a novalidate attribute will be added to the containing form in validate elements.
1408+
* @param options.delayedValidation If set to false (the default), validation happens while user inputs. If set to true, validation happens on blur, unless input is already invalid, in which case it will validate on input to indicate the value is valid as soon as possible.
13871409
*/
13881410
bootstrap(options?: Partial<ValidationServiceOptions>) {
13891411
Object.assign(this.options, options);

0 commit comments

Comments
 (0)