T
TanStack•2mo ago
genetic-orange

How to clear field errors onChange?

Hello! 👋 I’m using TanStack Form for the first time and trying to achieve the following validation flow: 1. User types into a field. 2. Validation runs only on blur. 3. If there’s an error, and the user starts typing again, the error should clear immediately (on change). 4. On the next blur, validation should re-run. I tried resetting the error by returning undefined from the onChange validator, but it doesn’t seem to have any effect. Is there a recommended way to clear field errors when the user starts typing? Here’s a simplified example that I've tried (without success):
<form.Field
name="activityType"
validators={{
onChange: () => undefined,
onBlur: ({ value }) =>
validateRequiredField(value, "Activity type"),
}}
>
// Returns error "Activity type required" using a custom `validateRequiredField` function
<form.Field
name="activityType"
validators={{
onChange: () => undefined,
onBlur: ({ value }) =>
validateRequiredField(value, "Activity type"),
}}
>
// Returns error "Activity type required" using a custom `validateRequiredField` function
Any ideas on how to reset the error state correctly on change? Thanks! 🙏
1 Reply
rare-sapphire
rare-sapphire•2mo ago
I think you need to throw field.listeners.onChange into the mix to clear errors again (3.)

Did you find this page helpful?