add touched check to validator
This commit is contained in:
parent
f2e6259bb7
commit
cd2b5128f5
|
@ -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]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue