IMPR: Add validate form/field functionality

This commit is contained in:
Tony Air 2024-06-26 03:32:43 +02:00
parent 8f55fa6ef4
commit 2fc7882cf7
2 changed files with 18 additions and 8 deletions

View File

@ -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

View File

@ -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