Validation Guide
React Cool Form supports a wide range of synchronous and asynchronous validation strategies for built-in, form-level, and field-level validation to cover all the cases that you need.
Built-in Validation#
We support HTML form validation out of the box, a quick and easy way for form validation.
Some validation attributes such as minLength, maxLength, min, and max are designed to validate a field once it has been edited by the user. Therefore when manually trigger these validations, use the pattern attribute or custom validation instead.
Form-level Validation#
The validate option provides a convenient way to access the complete values of the form (a.k.a formState.values), which is useful to validate dependent fields at the same time.
๐ก Please ensure the shape of the
errorsmatches the shape of form'svalues. If you're dealing with complex form data, we've provided a set of utility functions to help you get shit done ๐ฉ.
In addition to write your own logic, it's also possible to use a 3rd-party library such as Yup, Joi, and many others with form-level validation. Let's take a look at the following example:
๐ Looking for the example of Joi? Right here.
Field-level Validation#
React Cool Form provides the field method for field-level validation. Simply register your validator via the ref attribute of a field like the following example:
The field method can not only be used for validating but also for converting data type. When they are used together, just tweak the code as below:
Manually Triggering Validation#
We can manually trigger built-in, field-level, and form-level validation with the validateField and validateForm methods respectively. Here I take form-level as an example:
When/How Does Validation Run?#
By default, React Cool Form runs all the validation methods as follows. You can tell React Cool Form when to run validation by changing the validateOnChange and/or validateOnBlur depends on your needs.
When to Run#
| Event/method | Target | Timing |
|---|---|---|
onChange | Individual | Whenever the value of a field has been changed. |
setFieldValue | Individual | Whenever the value of a field has been set. |
setValues | All | Whenever the values of the formState has been set. |
onBlur | Individual | Whenever a field has been touched. If a validation method has been run by the onChange event, it won't be run again. |
onSubmit | All | Whenever a submission attempt is made. |
submit | All | Whenever a submission attempt is made manually. |
validateField | Individual | Manually run validation for a single field. |
validateForm | All | Manually run validation for the form. |
How to Run#
When validating with mixed ways, the results are deeply merged according to the following order:
- Built-in validation
- Field-level validation
- Form-level validation
๐ก To make the validation result of each field works correctly via the individual target event or method. When using form-level validation, please ensure the shape of the
errorsmatches the form'svalues.
Displaying Error Messages#
All errors are stored in the formState.errors, we can display error messages by accessing the errors object via the getState method. The getState method is designed to filter the errors of untouched fields by default, which based on the Errors in Forms design guideline (No.7). You can disable the feature by setting the filterUntouchedError option to false.