I have a multi-page react-hook-form form and I’m running into a validation issue I can’t seem to wrap my head around. I’m trying to only validate the page the user is on, not pages they haven’t made it to yet.
Here’s a CodeSandbox
If you enter something into the input and click "Next", you can move on to the next page. If you click "Back" and then try to click "Next" again, it won’t let you because it’s validating the second page but the user isn’t on that page.
I should be able to freely go between page 1 and 2 even though I haven’t added an item to my field array. It should only validate if I try to click "Next" on the 2nd page.
2
Answers
Solution
You should be using separate forms for each page and collect all the different forms’ data using either a 3rd party library for this or some custom-defined higher state. This is the recommended approach by the official
react-hook-form
docs, see Advanced Usage > Wizard Form / Funnel.Explanation of the problem itself
The problem you’re having is that when you click "Next", it invokes
handleSubmit
which validates the data prior to invoking theonSubmit
callback (see docs).This is not a problem when you first load the page with
PageOne
, as it includes only a required input field that you populate prior to clicking "Next" for the first time, but it becomes a problem oncePageTwo
loads – it add a requireditems
field to the form which is invalid by default until you add an actual item to it.You should only ever use
handleSubmit
if you intend to validate and submit the all of the form data; you should not use it to open/load another part of the same form (emphasis on "same").I ran into a similar problem and solved it by making each page its own form. Since I needed to submit all the pages together, the submit action on each form saved the data to a ref and then the last page actually submitted the data to my API.
It looked something like this:
MultiForm.jsx
PageOne.jsx
PageTwo.jsx