Skip to content

Should the 'required' prop on a field be set according to the zod schema? If so, how to do it? #747

Answered by DKeen0123
DKeen0123 asked this question in Q&A
Discussion options

You must be logged in to vote

Aha, I figured it out:

you need to set the constraint on the useForm hook.

for me, that looked like:


  const [form, fields] = useForm({
    // Sync the result of last submission
    lastResult,
    constraint: getZodConstraint(FormSchema),
    // Reuse the validation logic on the client
    onValidate({ formData }) {
      return parseWithZod(formData, { schema: FormSchema })
    },

    // Validate the form on blur event triggered
    shouldValidate: 'onBlur',
  })

Hope this helps anyone else curious about this!

I'm also curious as to why this is an option vs just the default behaviour? I can't imagine not wanting this behaviour 🤔

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by DKeen0123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant