FIX: form/field validation extra checks repeated funcs

This commit is contained in:
Tony Air 2024-06-26 03:43:08 +02:00
parent 2fc7882cf7
commit 52cade10c8
2 changed files with 8 additions and 16 deletions

View File

@ -3,7 +3,7 @@ const NAME = 'ui.validate.field'
class ValidateField { class ValidateField {
#field #field
#extraChecks = [] #extraChecks = {}
constructor(field) { constructor(field) {
this.#field = field this.#field = field
@ -26,10 +26,7 @@ class ValidateField {
} }
addExtraCheck = (validateFunc) => { addExtraCheck = (validateFunc) => {
if (!this.#extraChecks.includes(validateFunc)) { this.#extraChecks[validateFunc.name] = validateFunc
this.#extraChecks.push(validateFunc)
}
return this return this
} }
@ -43,10 +40,8 @@ class ValidateField {
// run extra checks // run extra checks
let valid = true let valid = true
console.log(this.#extraChecks) for (const validateFuncName in this.#extraChecks) {
valid = this.#extraChecks[validateFuncName](this.#field)
for (const validateFunc of this.#extraChecks) {
valid = validateFunc(this.#field)
if (!valid) { if (!valid) {
break break

View File

@ -6,7 +6,7 @@ const NAME = 'ui.validate.form'
class ValidateForm { class ValidateForm {
#steppedUI #steppedUI
#form #form
#extraChecks = [] #extraChecks = {}
constructor(form) { constructor(form) {
this.#form = form this.#form = form
@ -61,10 +61,7 @@ class ValidateForm {
} }
addExtraCheck = (validateFunc) => { addExtraCheck = (validateFunc) => {
if (!this.#extraChecks.includes(validateFunc)) { this.#extraChecks[validateFunc.name] = validateFunc
this.#extraChecks.push(validateFunc)
}
return this return this
} }
@ -87,8 +84,8 @@ class ValidateForm {
} }
// run extra checks // run extra checks
for (const validateFunc of this.#extraChecks) { for (const validateFuncName in this.#extraChecks) {
valid = validateFunc(this.#form) valid = this.#extraChecks[validateFuncName](this.#form)
if (!valid) { if (!valid) {
break break