diff --git a/index.html b/index.html index f86e483..74c2ac8 100644 --- a/index.html +++ b/index.html @@ -1,13 +1,23 @@ - - - - + + + + Vite + TS - - -
- - + + +
+
+
+ + + + + +
+
+
+ + diff --git a/src/Validator/index.ts b/src/Validator/index.ts index cc1bf9f..87dec1f 100644 --- a/src/Validator/index.ts +++ b/src/Validator/index.ts @@ -63,7 +63,7 @@ export class Validator { * @param {string} fieldValue * @private */ - private runValidator(cbName: string, field: string, fieldValue: string): void { + private runValidator(cbName: string, field: string, fieldValue: any): void { const rule = this.rules[cbName] if(rule.cb(fieldValue, ...rule.optionalParams ?? [])) { @@ -78,11 +78,8 @@ export class Validator { for (const field in this._rulesMapping) { this._rulesMapping[field].forEach((cbName) => { const fieldValue = this.form.get(field) - if (!fieldValue) { - return - } - this.runValidator(cbName, field, fieldValue.toString()) + this.runValidator(cbName, field, fieldValue) }) } } diff --git a/src/main.ts b/src/main.ts index 4c0ae9d..7b57801 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,31 +3,18 @@ import {Validator} from "./Validator"; import {required} from "./rules/required"; import {length} from "./rules/length"; -document.querySelector('#app')!.innerHTML = ` -
-
- - - -
-
-` - const form = document.querySelector('form')! form.addEventListener('submit', (e) => { e.preventDefault() const formData = new FormData(form); - const data: Record = {name: "name", phone: '0123456789'} - for (const key in data) { - formData.append(key, data[key]) - } const validator = new Validator(formData) validator.addRule('required', required) validator.addRule('length', length, 10) validator.setRules('name', ['required', 'length']) + validator.setRules('firstname', ['required', 'length']) validator.initErrorMessages({ required: 'Le champ {champ} est requis', length: "Le champ {champ} est trop long"