From acaf129d09f83f82dc85261393d50bfa3160dfca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Kottal?= Date: Wed, 17 Nov 2021 14:14:37 +0100 Subject: [PATCH 1/2] adds isValid(form) method for checking whether a form is valid --- src/index.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/index.ts b/src/index.ts index 47edca5..236fb5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -602,6 +602,23 @@ 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; + } + /** * Tracks a
element as parent of an input UID. When the form is submitted, attempts to validate the said input asynchronously. * @param form From 968c9d75882a88e9a13cb08933269fd8f43b859f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Kottal?= Date: Wed, 17 Nov 2021 15:16:45 +0100 Subject: [PATCH 2/2] adds isFieldValid(field) method for checking whether a field is valid --- src/index.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/index.ts b/src/index.ts index 236fb5c..5d5ff33 100644 --- a/src/index.ts +++ b/src/index.ts @@ -619,6 +619,26 @@ export class ValidationService { 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 element as parent of an input UID. When the form is submitted, attempts to validate the said input asynchronously. * @param form