Integrating with Formspree #3314
Replies: 1 comment
-
I started working to extend the example above to display validation errors from react-hook-form and Formspree together. However I realized part way through that it doesn't make much sense to mix the frontend and backend errors. If you use react-hook-form you should do thorough enough frontend validation to ensure no backend errors are returned. The backend validation is there as a backup or for cases where validation can only happen on the server side (like in case of card failure with our Stripe plugin). But those form-wide server errors won't have a field associated with them and should be displayed at the bottom of the form, separate from react-hook-form's validation errors. Regardless I spent some time figuring out how you could merge both the frontend and backend errors. It could be useful in the future in case Formspree enables more sophisticated backend validation that can only be performed server-side, like uniqueness checks. If interested, see below. Formspree returns an array of errors that looks like this:
I'm thinking I could first merge both react-hook-form errors and Formspree errors into a single error object like this:
Then you could just display errors as usual:
Form-wide errors returned by Formspree don't have a
But if your form contained a field named Here's a final example that shows how you could merge Formspree server errors and react-hook-form client side errors: https://codesandbox.io/s/react-hook-form-formspree-ini64 |
Beta Was this translation helpful? Give feedback.
-
I created an example that shows how to integrate react-hook-form with Formspree to collect form submissions.
Here's the codesandbox: https://codesandbox.io/s/react-hook-form-formspree-basic-euzlo
Beta Was this translation helpful? Give feedback.
All reactions