How can I lift validation errors up to the parent object? #1305
Unanswered
antoinerey
asked this question in
Q&A
Replies: 1 comment
-
I believe the root cause of the issue is the The only thing would be to rely on the const form = useForm({
// ...
validators: {
onChange: z.object({
price: z.object({
amount: z.string(),
currency: z.litteral('EUR'),
}).refine(price => price.amount.length > 1, {
message: 'Field is required',
}),
}),
},
}) But I feel like it kind of defeats the purpose of Zod. Most of the validation would then happen in the Perhaps there's another way with Zod? Or maybe the solution lives in TanStack Form? 🤷 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
👋 Hey!
So, I have an issue with validating deep objects. The form validation as a whole works as expected, but I'm getting trouble associating validation errors with the part of the form I have in mind.
Take the following form:
I consider prices as a single entity with two properties:
amount
andcurrency
. I pass them around in the codebase as an "opaque" object, and I built a customPriceInput
that works the same way.It works great. When typing a new amount in the
PriceInput
, it gets reflected into theform
values. Then, I tried to add validations into the mix.The validation works fine in the sense that it catches issues, and disables the form. However, the error gets associated with
price.amount
and notprice
(as I would like to). Because of that, thefield.state.meta.errors
passed to thePriceInput
never gets updated, and stays empty.So my question is: Is there a way to associate "deep" errors with their parent field instead?
Here is a live example of the issue I'm bumping into: https://stackblitz.com/edit/vitejs-vite-naafteya?file=src/PriceInput.tsx,src/App.tsx.
Beta Was this translation helpful? Give feedback.
All reactions