Keeps and validates your form's data. Doesn't mess with your components. Simple and straight to the point.
- Keeps form's state and validation results
- Supports any kind of validation functions
- Dirty checking
- Separates data from view
- Relies on hooks, but can easily be used with class components
Package can be installed with one of the following commands (depending on whether you prefer npm
or yarn
)
npm install @enterwell/react-form-validation
yarn add @enterwell/react-form-validation
2 quick steps to validate any kind of input.
Step 1:
- Define validation function (or use some of predefined) and use it to initialize validation hook
const emailValidation = (value) => /^.+@\S+\.\S+$/.test(value);
const email = useValidation('matej.radovix@enterwell.net', emailValidation);
Step 2:
- Pass the email data to your input
<div>
<input
className={email.error ? 'error' : 'no-error'}
value={email.value}
onChange={email.onChange}
onBlur={email.onBlur}
>
{email.error && (
<span className="error-message">Incorrect email!</span>
)}
</div>
And thats all!
Hook that keeps on form field's data.
Name | Type | Required | Description |
---|---|---|---|
initialValue | any | yes | Field's initial value |
validationFn | (any) => boolean or Promise<boolean> | yes | Function for validating the field's value |
config | { receiveEvent: boolean, reversed: boolean ignoreDirtiness: boolean } |
no | Additional hook configuration.
|
Type | Description |
---|---|
{ value: any, error: boolean onChange: (any) => void onBlur: () => void validate: (any) => boolean or Promise<boolean> reset: () => void } |
Object with field's data and callbacks.
|
import { useValidation, isNonEmptyString } from '@enterwell/react-form-validation';
/* Some code... */
const userFormData = {
name: useValidation(name, isNonEmptyString),
age: useValidation(age, () => true)
};
console.log(userFormData.name) // {value: "Matej", error: false, onChange: ƒ, onBlur: ƒ, validate: ƒ, reset: f}"
Util function for extracting values from fields' data objects.
Name | Type | Required | Description |
---|---|---|---|
fields | { value: any } | yes | Form field's data (each field must have value property - other properties are not important) |
Type | Description |
---|---|
Object | Object with fields' names as keys, and their values as values |
import { ..., extractValues } from '@enterwell/react-form-validation';
/* useValidation example's code... */
const data = extractValues(userFormData);
console.log(data) // {name: "Matej", age: 10}
Util function for validating values of all fields
Name | Type | Required | Description |
---|---|---|---|
fields | { value: any validate: (any) => boolean } |
yes | Form field's data (each field must have value and validate properties - other properties are not important) |
Type | Description |
---|---|
boolean or Promise<boolean> | false if form data is correct, true otherwise. Promise with same result when at least one validation function resolved to Promise. |
import { ..., validateFields } from '@enterwell/react-form-validation';
/* useValidation example's code... */
const hasError = validateFields(userFormData);
console.log(hasError) // false
Util function for resetting all fields' data.
Name | Type | Required | Description |
---|---|---|---|
fields | { reset: () => void } | yes | Form field's data (each field must have reset property - other properties are not important) |
import { ..., resetFields } from '@enterwell/react-form-validation';
/* useValidation example's code... */
resetFields(userFormData);
Util function for handling the form submit. Form's fields are first validated. If all values are correct, they are extracted to data object and passed to onSubmit
callback.
Name | Type | Required | Description |
---|---|---|---|
fields | { value: any validate: (any) => boolean } |
yes | Form field's data (each field must have value and validate properties - other properties are not important) |
onSubmit | (any) => void | yes | On submit callback |
import { ..., submitForm } from '@enterwell/react-form-validation';
/* useValidation example's code... */
const onSubmit = (data) => fetch("www.some-url.com/post", {
method: 'POST',
data: JSON.stringify(data)
});
submitForm(userFormData, onSubmit);
Util function for handling the form cancel. Form's fields are reset and onCancel
callback is called.
Name | Type | Required | Description |
---|---|---|---|
fields | { reset: () => void } | yes | Form field's data (each field must have reset property - other properties are not important) |
onCancel | (any) => void | yes | On cancel callback |
import { ..., cancelForm } from '@enterwell/react-form-validation';
/* useValidation example's code... */
const onCancel = (data) => alert("Form has been reset");
cancelForm(userFormData, onCancel);
Unless otherwise stated, each validation function will have the following #### Params and #### Returns.
Name | Type | Required | Description |
---|---|---|---|
value | any | no | Form field's value |
Type | Description |
---|---|
boolean | true if field's value is correct, false otherwise |
No error validation. Value of the field with this validation function will always be correct.
Are values equal validation. Value of the field with this validation function will be correct if it is equal to some other value (e.g. some other field's value).
Name | Type | Required | Description |
---|---|---|---|
value | any | no | Form field's value |
other | any | no | Some other value |
Type | Description |
---|---|
boolean | true if field's value is correct, false otherwise |
Is true validation. Value of the field with this validation function will be correct only if it is true
.
Is not null validation. Value of the field with this validation function will be correct only if it is not null
.
Is non-empty string validation. Value of the field with this validation function will be correct if it is not an empty string.
Is valid number validation. Value of the field with this validation function will be correct if it represents the valid number (as number or as string).
Is positive number validation. Value of the field with this validation function will be correct if it is the positive number.
Is negative number validation. Value of the field with this validation function will be correct if it is the negative number.
Is non-empty array validation. Value of the field with this validation function will be correct if it is the non-empty array.
We noticed that there are few things which could be added to our package in order to make it more flexible and easy to use. Here is what you can expect in the future:
- additional config which will enable to change when value is validate (e.g. only in
onChange
, inonBlur
but only if current value is not valid and so on) - more validation functions