Skip to content

Commit ad9246e

Browse files
committed
Only validate field if not hidden
1 parent cef508d commit ad9246e

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/index.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -831,45 +831,60 @@ export class ValidationService {
831831
*/
832832
createValidator(input: HTMLInputElement, directives: ValidationDirective) {
833833
return async () => {
834-
for (let key in directives) {
835-
let directive = directives[key];
836-
let provider = this.providers[key];
837834

838-
if (!provider) {
839-
console.log('aspnet-validation provider not implemented: ' + key);
840-
continue;
841-
}
835+
// only validate visible fields
836+
if (!this.isHidden(input))
837+
{
838+
for (let key in directives) {
839+
let directive = directives[key];
840+
let provider = this.providers[key];
842841

843-
let result = provider(input.value, input, directive.params);
844-
let valid = false;
845-
let error = directive.error;
842+
if (!provider) {
843+
console.log('aspnet-validation provider not implemented: ' + key);
844+
continue;
845+
}
846846

847-
if (typeof result === 'boolean') {
848-
valid = result;
849-
} else if (typeof result === 'string') {
850-
valid = false;
851-
error = result;
852-
} else {
853-
let resolution = await result;
854-
if (typeof resolution === 'boolean') {
855-
valid = resolution;
856-
} else {
847+
let result = provider(input.value, input, directive.params);
848+
let valid = false;
849+
let error = directive.error;
850+
851+
if (typeof result === 'boolean') {
852+
valid = result;
853+
} else if (typeof result === 'string') {
857854
valid = false;
858-
error = resolution;
855+
error = result;
856+
} else {
857+
let resolution = await result;
858+
if (typeof resolution === 'boolean') {
859+
valid = resolution;
860+
} else {
861+
valid = false;
862+
error = resolution;
863+
}
859864
}
860-
}
861865

862-
if (!valid) {
863-
this.addError(input, error);
864-
return false;
866+
if (!valid) {
867+
this.addError(input, error);
868+
return false;
869+
}
865870
}
866871
}
867872

868873
this.removeError(input);
869874
return true;
875+
870876
};
871877
}
872878

879+
/**
880+
* Checks if the provided input is hidden from the browser
881+
* @param input
882+
* @returns
883+
*/
884+
private isHidden(input: HTMLElement) {
885+
return !!( input.offsetWidth || input.offsetHeight || input.getClientRects().length );
886+
}
887+
873888
/**
874889
* Load default validation providers and scans the entire document when ready.
875890
*/

0 commit comments

Comments
 (0)