How to determine if a field has actually been validated? (isDirty/isValid not sufficient with debounce) #1994
Replies: 1 comment
|
You're not missing anything — there's no Nothing to key off. Your reading of each flag was right. Worth saving you the detour on the last one you listed, though, because it looks like it works and doesn't. which reads like a "this validator has run" marker. But the key is written when a validator produces an error, not when it runs. A field whose validator passes on the first attempt never gets one: So it's really "has ever errored", and it would silently fail for exactly the fields you want the tick on — the ones that were correct first time. ( What does work is recording it yourself, using const field = useField({
name: 'email',
listeners: {
onChange: () => setHasValidated(false),
},
validators: {
onChangeAsyncDebounceMs: 500,
onChangeAsync: async ({ value }) => {
const error = await checkEmail(value)
setHasValidated(true)
return error
},
},
})
const showCheckmark = hasValidated && field.state.meta.isValid
Which is your spec exactly, including the reset in [6]. The ordering holds because the listener runs before the validator on every change — 3 listener fires, 3 validator runs, strictly alternating. Two practical notes. Put the flag in a ref or state rather than a module-level variable if you have more than one instance of the field on screen, since it's per-field state. And if you have many fields, it's less repetitive to keep a As a feature request I think it stands up — the flag is derivable but only from inside the validator, which means every validator has to opt in, and a |
Uh oh!
There was an error while loading. Please reload this page.
Problem
I'm trying to show a validation success icon (✓) next to fields that have been validated successfully. The challenge is distinguishing between "validation passed" and "validation hasn't run yet."
With debounced async validation, isValid is true by default because there are no errors, not because validation actually passed. Combined with isDirty or isTouched, this still doesn't tell me whether the debounced validator has actually executed.
What I tried
Desired behavior
Something like a hasBeenValidated or isValidated flag that:
Questions
Thanks!
All reactions