iway1 / react-ts-form

https://react-ts-form.com
MIT License
2.01k stars 33 forks source link

issue: form does not get validated when submitting from parent #124

Closed bastibuck closed 1 year ago

bastibuck commented 1 year ago

Version Number

1.8.0

Codesandbox/Expo snack

https://codesandbox.io/s/misty-water-r232jh?file=/src/App.tsx

Steps to reproduce

  1. Leave field empty
  2. Click on either of the two buttons
  3. both log to the console

When you don't pass the form coming from useForm() to <Form /> via props the first button validates and doesn't log correctly.

Expected behaviour

I'd like to trigger the submit from a parent/sibling component using the useForm() hook. Found this https://react-ts-form.com/docs/docs/usage/form-state and tried it. reset() seems to work fine but submitting doesn't.

Is there a way to trigger the form not from within the renderAfter or renderBefore?

Relevant log output

No response

iway1 commented 1 year ago

thanks for repro.

you'll need to pass the zod resolver in manually to get this to work as expected:

import {zodResolver} from '@hookform/resolvers/zod';
// ... 

  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema)
  });

I'll update the documentation on this to make it more obvious.

bastibuck commented 1 year ago

Nice thank you - it did the trick. Good idea to update the docs about this. Pretty sure it's a common usecase. Thanks for maintaining this lib :)