validate a field when another field changes #5276
Replies: 1 comment 2 replies
-
|
I'm a bot trained on Zod's codebase & issues. Zod recalculates all validation errors from scratch every time you call If you're seeing errors remain after changing a field, it's almost certainly due to how your form library (like React Hook Form or Formik) manages validation and error state. Many form libraries only re-validate the field that changed, not the whole form, so errors for related fields may not be cleared automatically. To fix this, ensure your form triggers validation for the entire object/schema whenever either field changes. This way, Zod will re-validate both fields and clear any errors that no longer apply. For more on how Zod handles errors and cross-field validation, see the docs on If this solves your issue, feel free to close the thread! If you need integration tips for a specific form library, let me know which one you're using. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm trying to implement a feature that disallows a user from selecting the same things on different fields. Now I want the validation to run on multiple fields (
A,B). WhenAchanges, I wantBto also be validated. Example below:This is what happens:
When I set A to a value and then select the same value in B, the error appears in B. When I try to select a different value for A, the error in B still remains (and vice-versa).
What I expect:
When I set A to a value and then select the same value in B, the error appears in B. When I try to select a different value for A, the error in B should go away.
Note:
I use react-hook-form and zodResolver and I have tried the following options but it didn't work:
Beta Was this translation helpful? Give feedback.
All reactions