Form validation defaults #1279
-
Hi guys, I would like to know how to replicate the defaults from react-hook-form in tanstack-form. What I would like to achieve is to define one zod schema, that will validate the form but only after form is initially submitted => then I would like to validate as user types so basically this is default from react-hook-form Is it easy to achieve in @tanstack/react-form or rather requires a lot of effort? I would be grateful for any basic example that can replicate it. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Is this something similar to what you're looking for? const form = useForm({
validators: {
onChange: ({ formApi }) =>
formApi.state.submissionAttempts > 0 ? yourValidator : undefined,
onSubmit: ({ formApi }) => formApi.state.submissionAttempts == 0 ? yourValidator : undefined
},
}); If you're validating with a schema library like zod or valibot, you can use |
Beta Was this translation helpful? Give feedback.
-
React Hook Form by default does not append errors form the form level schema unless the user actually touches the field. This makes it so users can't really control when the error is appended (They abstract that away). TSF differs in that we ALWAYS append the errors to the corresponding errorMap/errors array for each field. You then have full control of when to display the error by using field.isTouched or similar to what Balastrong suggested using form.state.submissionAttempts. |
Beta Was this translation helpful? Give feedback.
-
I'm also interested in a better way to achieve this with Tanstack Form. The current workaround isn't really developer friendly as you have to repeat your validator twice, and repeat it every time you use |
Beta Was this translation helpful? Give feedback.
Is this something similar to what you're looking for?
If you're validating with a schema library like zod or valibot, you can use
formApi.parseValuesWithSchema(yourSchema)
asyourValidator
in the snippet above