Skip to content

Commit

Permalink
Merge pull request #125 from iway1/iway1-patch-1
Browse files Browse the repository at this point in the history
Update form-state.md
  • Loading branch information
iway1 authored Jun 19, 2023
2 parents 75dcec9 + 009acec commit fb146c2
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion www/docs/docs/usage/form-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ sidebar_position: 4
Sometimes you need to work with the form directly (such as to reset the form from the parent). In these cases, just pass the `react-hook-form` `useForm()` result to your form:

```tsx
import {zodResolver} from '@hookform/resolvers/zod';
import {z} from 'zod';

const FormSchema = z.object({
myTextField: z.string().min(10, 'must be 10 in length')
})

function MyPage() {
// Need to type the useForm call accordingly
const form = useForm<z.infer<typeof FormSchema>>();
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema)
});
const { reset } = form;
return (
<Form
Expand All @@ -22,3 +31,5 @@ function MyPage() {
);
}
```

Notice we have to import `zodResolver` and pass it as the resolver to useForm in order to have our form be validated properly (this is something the library normally does internally).

1 comment on commit fb146c2

@vercel
Copy link

@vercel vercel bot commented on fb146c2 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.