From 2fc7882cf7b1e08d859c7dfd39dc17c96554fd02 Mon Sep 17 00:00:00 2001 From: Tony Air Date: Wed, 26 Jun 2024 03:32:43 +0200 Subject: [PATCH] IMPR: Add validate form/field functionality --- src/js/ui/validate.field.js | 14 ++++++++++---- src/js/ui/validate.form.js | 12 ++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/js/ui/validate.field.js b/src/js/ui/validate.field.js index 62695ca..61141b4 100644 --- a/src/js/ui/validate.field.js +++ b/src/js/ui/validate.field.js @@ -25,8 +25,12 @@ class ValidateField { this.#field.dispatchEvent(new Event(Events.FORM_INIT_VALIDATE_FIELD)) } - addExtraCheck = (func) => { - this.#extraChecks.push(func) + addExtraCheck = (validateFunc) => { + if (!this.#extraChecks.includes(validateFunc)) { + this.#extraChecks.push(validateFunc) + } + + return this } validate = () => { @@ -39,8 +43,10 @@ class ValidateField { // run extra checks let valid = true - for (const func in this.#extraChecks) { - valid = func(this.#field) + console.log(this.#extraChecks) + + for (const validateFunc of this.#extraChecks) { + valid = validateFunc(this.#field) if (!valid) { break diff --git a/src/js/ui/validate.form.js b/src/js/ui/validate.form.js index 434186c..94a49e4 100644 --- a/src/js/ui/validate.form.js +++ b/src/js/ui/validate.form.js @@ -60,8 +60,12 @@ class ValidateForm { } } - addExtraCheck = (func) => { - this.#extraChecks.push(func) + addExtraCheck = (validateFunc) => { + if (!this.#extraChecks.includes(validateFunc)) { + this.#extraChecks.push(validateFunc) + } + + return this } validate = async () => { @@ -83,8 +87,8 @@ class ValidateForm { } // run extra checks - for (const func in this.#extraChecks) { - valid = func(this.#form) + for (const validateFunc of this.#extraChecks) { + valid = validateFunc(this.#form) if (!valid) { break