Skip to content

Commit

Permalink
fix: errors aren't defined when form is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel committed Sep 5, 2022
1 parent 83c9aad commit 18e261f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
28 changes: 19 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<head>
<meta charset="UTF-8"/>
<link rel="icon" type="image/svg+xml" href="/vite.svg"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</head>
<body>
<div id="app">
<div>
<form action="">
<label for="name">name</label>
<input type="text" name="name" id="name">
<label for="firstname">firstname</label>
<input type="text" name="firstname" id="firstname">
<button type="submit">submit</button>
</form>
</div>
</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
7 changes: 2 additions & 5 deletions src/Validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [])) {
Expand All @@ -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)
})
}
}
Expand Down
15 changes: 1 addition & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@ import {Validator} from "./Validator";
import {required} from "./rules/required";
import {length} from "./rules/length";

document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
<div>
<form action="">
<label for="name">name</label>
<input type="text" name="name" id="name">
<button type="submit">submit</button>
</form>
</div>
`

const form = document.querySelector('form')!
form.addEventListener('submit', (e) => {
e.preventDefault()

const formData = new FormData(form);
const data: Record<string, string> = {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"
Expand Down

0 comments on commit 18e261f

Please sign in to comment.