T
TanStack6mo ago
national-gold

Can I add more on an validator onChange besides zod validator?

Currently the docs show this:
validators: {
// DEMO: You can switch between schemas seamlessly
onChange: ZodSchema,
// onChange: ValibotSchema,
// onChange: ArkTypeSchema,
},
validators: {
// DEMO: You can switch between schemas seamlessly
onChange: ZodSchema,
// onChange: ValibotSchema,
// onChange: ArkTypeSchema,
},
but I want to add some other logic such as recording some logic in a store!
validators: {
onChange: ({ value }) => {
// Update the store with new values
listingStore.setState((state) => ({
...state,
values: { ...state.values, ...value },
}));

//do zod stuff here?
}
validators: {
onChange: ({ value }) => {
// Update the store with new values
listingStore.setState((state) => ({
...state,
values: { ...state.values, ...value },
}));

//do zod stuff here?
}
But i am not sure how to accomplish that. Anyone has any ideas?
5 Replies
ratty-blush
ratty-blush6mo ago
zod always allows you to manually handle it too in this case doing schema.safeParse(value) and checking if success is false
stormy-gold
stormy-gold6mo ago
Side effects for event triggers | TanStack Form React Docs
For situations where you want to "affect" or "react" to triggers, there's the listener API. For example, if you, as the developer, want to reset a form field as a result of another field changing, you...
national-gold
national-goldOP6mo ago
Luca's answer helped. I didn't go the listener route, because I don't want to place a listener on each field since i have a ton
ratty-blush
ratty-blush6mo ago
if you could turn the zod validator into a reusable function, you can probably do the same with listeners. It's up to you though.
national-gold
national-goldOP6mo ago
Yeah totally, appreciate your help!

Did you find this page helpful?