ReactHookForm Resolver
ForgeForm is designed to integrate flawlessly with React Hook Form, a popular library for form state management in React. To bridge ForgeForm's schema-based validation with React Hook Form's form handling, ForgeForm provides a custom resolver called forgeFormResolver.
Contents
What is a Resolver in React Hook Form?
Installation
ForgeForm Resolver: forgeFormResolver
Basic Usage Example
Explanation of the Example
Benefits of Using forgeFormResolver
Detailed Examples and Use Cases
Example 1: Form with Various Field Types and Validations using Resolver
Example 2: Handling Validation Modes and Resolver
Example 3: Resolver with Nested Fields and Dot Notation
Error Handling with forgeFormResolver
What is a Resolver in React Hook Form?
In React Hook Form, a resolver is a plugin that connects external validation libraries (like Yup, Zod, Joi, and now ForgeForm) to React Hook Form's form validation process. The resolver acts as an adapter: it takes the form data from React Hook Form, passes it to the external validation library for validation based on a defined schema, and then translates the validation results back into a format that React Hook Form can understand and use to display errors and manage form validity.
Installation
To use React Hook Form with ForgeForm, you need to install the react-hook-form package. You can do this using npm or yarn:
ForgeForm Resolver: forgeFormResolver
forgeFormResolver is the bridge that allows you to use your ForgeForm schemas directly within React Hook Form for validation. It enables you to define your form structure and validation rules using ForgeForm's intuitive schema DSL, and then seamlessly apply these rules to your React Hook Form forms.
Basic Usage Example:
Here's a basic example demonstrating how to integrate forgeFormResolver with useForm in React Hook Form:
Explanation of the Example:
-
Import
forgeFormResolver:Import the resolver function from the ForgeForm library.
-
Define ForgeForm Schema:
Create your validation schema using
createSchemaas you normally would in ForgeForm. -
Integrate Resolver in
useForm:Within React Hook Form's
useFormhook, pass theresolverSchematoforgeFormResolver()and assign the result to theresolveroption. This tells React Hook Form to use ForgeForm for validation based on the provided schema. -
Register Input Fields:
Register your form input fields using React Hook Form's
registerfunction as usual. The field names (e.g.,'firstName','lastName') must match the field names defined in your ForgeForm schema. -
Display Errors:
React Hook Form's
errorsobject will now be populated with validation errors from ForgeForm. You can access these errors using dot notation (e.g.,errors.firstName.message) and display them in your UI.
Benefits of Using forgeFormResolver:
- Schema Reusability: Define your validation logic once in your ForgeForm schema and reuse it seamlessly in your React Hook Form components.
- Centralized Validation: Keep your validation rules separate from your component logic, promoting cleaner and more maintainable code.
- Leverage ForgeForm's Features: Take full advantage of ForgeForm's robust validation engine, built-in field types, custom validators, and sanitization capabilities within your React Hook Form forms.
- Simplified Form Logic: React Hook Form handles form state management and submission, while ForgeForm resolver handles validation, leading to a more streamlined development experience.
- Consistent Validation: Ensures consistent validation logic across your application by using a single source of truth – your ForgeForm schemas.
Detailed Examples and Use Cases:
Example 1: Form with Various Field Types and Validations using Resolver
This example builds upon the basic example to showcase more field types and validations within a React Hook Form integrated with forgeFormResolver:
Example 2: Handling Validation Modes and Resolver
React Hook Form offers different validation modes (onSubmit, onBlur, onChange, etc.). forgeFormResolver works seamlessly with these modes. You configure the mode in useForm options as usual, and the resolver will perform validation according to the chosen mode.
In this example, mode: 'onBlur' is set in useForm. Validation will now trigger when an input field loses focus (on blur). The trigger() function is also demonstrated to manually trigger validation when needed.
Example 3: Resolver with Nested Fields and Dot Notation
If your ForgeForm schema uses dot notation for nested fields (e.g., "address.city"), forgeFormResolver handles this structure correctly, mapping errors to the nested field paths in React Hook Form's errors object.
(You can refer back to the "React Form State Management" section's example that uses dot notation for nested fields; the resolver will work seamlessly with such schemas.)
Error Handling with forgeFormResolver:
When validation fails through forgeFormResolver, React Hook Form's errors object will be populated with error messages from ForgeForm. The structure of the errors object will directly correspond to your schema's field structure.
For example, if you have a schema like:
And validation fails, the errors object from React Hook Form might look like:
You can then access and display these errors in your component using paths like errors.email.message and errors.address.city.message.