T
TanStack7d ago
genetic-orange

Why can field validation overwrite form validation errors?

Not sure if this is the right place to ask, but I'm going over the docs for Form and came across this in the validation guide:
Something worth mentioning is that if you have a form validation function that returns an error, that error may be overwritten by the field-specific validation.
I was wondering what the rationale is for this behavior/limitation? errorMap already stores errors from different points in the form lifecycle (onBlur/onChange etc), why not also store form/field errors separately? Not that it needs a use case proving its worth, but something simple like this would break, no? 1. validate the entire form with something like Zod 2. use field-specific validation in the email field make sure the email exists
2 Replies
ratty-blush
ratty-blush7d ago
The rationale arises from a conflict of states and how they should be set. This is the current structure: Form errorMap: Specific to form-level, separated by source. Async variants of sources are merged with the sync ones. errors: Flattens the errorMap into an array. Fields errorMap: Specific to this field, separated by source. Forms can set field-level errors which cascade down to this property, but field validators can also set them. Since there's two sources of truth in that case, the field-level takes priority. errors: Flattens the errorMap into an array.
genetic-orange
genetic-orangeOP6d ago
got it, thanks for the in-depth look!

Did you find this page helpful?