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)) this.#field.dispatchEvent(new Event(Events.FORM_INIT_VALIDATE_FIELD))
} }
addExtraCheck = (func) => { addExtraCheck = (validateFunc) => {
this.#extraChecks.push(func) if (!this.#extraChecks.includes(validateFunc)) {
this.#extraChecks.push(validateFunc)
}
return this
} }
validate = () => { validate = () => {
@ -39,8 +43,10 @@ class ValidateField {
// run extra checks // run extra checks
let valid = true let valid = true
for (const func in this.#extraChecks) { console.log(this.#extraChecks)
valid = func(this.#field)
for (const validateFunc of this.#extraChecks) {
valid = validateFunc(this.#field)
if (!valid) { if (!valid) {
break break

View File

@ -60,8 +60,12 @@ class ValidateForm {
} }
} }
addExtraCheck = (func) => { addExtraCheck = (validateFunc) => {
this.#extraChecks.push(func) if (!this.#extraChecks.includes(validateFunc)) {
this.#extraChecks.push(validateFunc)
}
return this
} }
validate = async () => { validate = async () => {
@ -83,8 +87,8 @@ class ValidateForm {
} }
// run extra checks // run extra checks
for (const func in this.#extraChecks) { for (const validateFunc of this.#extraChecks) {
valid = func(this.#form) valid = validateFunc(this.#form)
if (!valid) { if (!valid) {
break break