add touched check to validator

This commit is contained in:
Keviin Åberg Kultalahti 2021-05-11 18:03:14 +02:00
parent b10c41143f
commit 46cbba1cc0
1 changed files with 8 additions and 1 deletions

View File

@ -1,9 +1,16 @@
import { writable, derived } from 'svelte/store' import { writable, derived } from 'svelte/store'
export function createValidationStore(initialValue, ...validators) { export function createValidationStore(initialValue, ...validators) {
let touched = false
const value = writable(initialValue || '') const value = writable(initialValue || '')
const error = derived(value, $v => validate($v, validators)) const error = derived(value, $v => {
if (touched) {
return validate($v, validators)
} else {
touched = true
}
})
return [value, error] return [value, error]
} }