Skip to content

Commit

Permalink
Merge pull request #8 from skttl/adds-isvalid-method
Browse files Browse the repository at this point in the history
Adds isValid(form) method for checking whether a form is valid
  • Loading branch information
haacked authored Nov 18, 2021
2 parents fbde6cd + 968c9d7 commit 2bfcde7
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,43 @@ export class ValidationService {
}
}

/**
* Returns true if the provided form is valid, and then calls the callback. The form will be validated before checking, unless prevalidate is set to false
* @param form
* @param prevalidate
* @param callback
* @returns
*/
isValid = (form: HTMLFormElement, prevalidate: boolean = true, callback: Function) => {
if (prevalidate) {
this.validateForm(form, callback);
}
let formUID = this.getElementUID(form);
let formInputUIDs = this.formInputs[formUID];
let invalidFormInputUIDs = formInputUIDs.filter(uid => this.summary[uid]);
return invalidFormInputUIDs.length == 0;
}

/**
* Returns true if the provided field is valid, and then calls the callback. The form will be validated before checking, unless prevalidate is set to false
* @param form
* @param prevalidate
* @param callback
* @returns
*/
isFieldValid = (field: HTMLElement, prevalidate: boolean = true, callback: Function) => {

if (prevalidate) {
let form = field.closest("form");
if (form != null) {
this.validateForm(form, callback);
}
}

let fieldUID = this.getElementUID(field);
return this.summary[fieldUID] != null;
}

/**
* Tracks a <form> element as parent of an input UID. When the form is submitted, attempts to validate the said input asynchronously.
* @param form
Expand Down

0 comments on commit 2bfcde7

Please sign in to comment.