Inconsistent validation timing - first field validates onBlur, subsequent fields validate onChange
Issue Title
TanStack Form: Inconsistent validation timing - first field validates onBlur, subsequent fields validate onChange
Problem: Form validation behavior is inconsistent across fields. The first text field correctly waits until blur to show validation errors, but subsequent text fields show validation errors immediately when the user starts typing.
Current Behavior:
- First field: ✅ Shows errors only after blur
- Subsequent fields: ❌ Shows errors immediately on typing
Configuration:
Composed Form Component
Question: Why would
field.state.meta.isTouched and !field.state.meta.isValid both be true during typing for some fields but not others, when using the same component and validation configuration?8 Replies
wise-whiteOP•5mo ago
One workaround I've found is to move all of the field validation inline into each form field.
However, this feels like it adds a bunch of extra boilerplate - especially for a large form
As an example, this worked (see below). But it required that I add the validation to each field individually.
dependent-tan•5mo ago
form validation isn't inconsistent across fields. It triggers on blur for any field and will generate the form errors which may include all fields.
The reason you only see it in the currently blurred field is because it's touched and not valid, and therefore matches the condition you set for it. Other fields have errors, but are not touched. Once you changed the field value, it's touched.
Are you perhaps looking for
field.state.meta.isBlurred?wise-whiteOP•5mo ago
Thanks @Luca | LeCarbonator!
I see. So in my case, it might make more sense to just add the validation at a field level?
dependent-tan•5mo ago
not necessarily. You can modify your
displayErrors to use isBlurred if you plan on using onBlur validation more often.
Since it's a field component, an unrelated note:
Meta properties should be accessed using useStore to ensure they're reactive.
The reason why is not in the documentation yet 😅 it's marked as issue #1207 thoughwise-whiteOP•5mo ago
Good to know! I'll give that a try!
@Luca | LeCarbonator—thanks again for your help! That ended up working out well.
One other question for you... So the fields properly validate when blurred now. However, when a user (myself) clicks "Submit", the fields don't show their invalid state. And it seems to be because they haven't been blurred. Any thoughts on how to best approach this?
This is what I currently have:
dependent-tan•5mo ago
right, I suggest an 'override' for submission attempts to ensure it's always showing errors for that:
wise-whiteOP•5mo ago
Would it also make sense to track the
isSubmitted like this?
dependent-tan•5mo ago
not particularly.
See the docs:
A boolean indicating if the onSubmit function has completed successfully. Goes back to false at each new submission attempt. Note: you can use isSubmitting to check if the form is currently submitting.