One react hook for all your form validations
npm install react-hooks-form-validatorSimple, extensible and config based form validation.
Small. 9 KB (minified and gzipped).
Size Limit controls the size.
- The problem
- This solution
- Installation
- Examples
- Basic Example
- Complex Example
- More Examples
- API Reference
- LICENSE
You want to write simple and maintainable form validations. As part of this goal, you want your validations to be simple yet accomadate your specifc needs. Be it in React Web or React Native. You should be able to add validations to more complicated components like Multi-Select, Date Time Picker. This also means it should easy to add validations with any design library you use,like MATERIAL-UI, Ant-D, React Bootstrap etc. or even if you don't use one. You should be able to validate form from your server or any async validations for that matter.
The React Use Form is a very lightweight solution for validating forms. It provides react hooks to configure your form, in a way that encourages simpler way to validate form. It doesn't assume anything about your design.
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies:
``
npm install --save react-hooks-form-validator
yarn add react-hooks-form-validator
`
This library has peerDependencies listings for react and react-dom.
> NOTE: The minimum supported version of react is ^16.9.0.
`jsx
import React from 'react';
import useForm from 'react-hooks-form-validator';
import { FormItem, Input, Button } from 'antd';
const formConfig = {
username: {
required: true,
min: 6,
},
password: {
min: 6,
max: 20,
required: true,
},
};
function FormComponent() {
const [fields, formData] = useForm(formConfig);
async function handleLogin() {
await login({
username: fields.username.value,
password: fields.password.value,
});
}
return (
export default FormComponent;
`
`jsx
import React from 'react';
import useForm from 'react-hooks-form-validator';
import { FormItem, Input, Button } from 'antd';
const formConfig = {
username: {
required: true,
min: 6,
patterns: [
{
regex: new RegExp(/[a-zA-Z0-9]+/),
errorMsg: 'Please enter a only alpha numeric username',
},
],
},
password: {
min: 6,
max: 20,
required: true,
},
confirmPassword: {
validate: (fieldValue, formValue) => {
const isPasswordSame = formValue.password === fieldValue;
return [isPasswordSame, 'Password and confirm password should be same'];
},
},
};
function FormComponent() {
const [fields, formData] = useForm(formConfig);
async function handleLogin() {
await login({
username: fields.username.value,
password: fields.password.value,
});
}
return (
export default FormComponent;
`
You'll find runnable examples in the react-hooks-form-validator-examples codesandbox.
Basic usage is like
const [fieldObject, formObject] = useForm(formConfig);
formConfig is object with key as fieldName and value as fieldConfig
`js`
{
field1: config1,
field2: config2,
field3: config3,
}
| key | What it does? | Type | Example |
| ------------ | :-----------------------------------: | :-------------------------------------------------: | -----------------------------------------------------------------------------------------------: |
| defaultValue | Default value of the field | any | '', [] |Boolean
| required | To set the field as required | or { errorMsg : String } | true or {errorMsg: 'This is required field' } |Number
| min | To set minimun length of field | or { errorMsg : String, length: Number} | 5 or {errorMsg: 'minimum of 5 character', length: 5} |Number
| max | To set maximum length of field | or { errorMsg : String, length: Number} | 5 or {errorMsg: 'maximim of 5 character', length: 5} |Array
| patterns | To validate against array of patterns | of {regex : RegExp, errorMsg: String} | [{regex: new RegExp(/[a-zA-Z0-9]+/), errorMsg: "Please enter a only alpha numeric username" }] |(fieldValue,formState) => [isValid, errorMessage]
| validate | Function To validate | | [(fieldValue, formState) => [!!formState.country.id, 'Country id is mandatory']] |
| key | What it does? | Type |
| ------------ | :------------------------------------------: | ------------------------------------------: |
| value | Current value of the field | any |
| errorMsg | Error message of the field | String |Boolean
| isValid | Validity of the field | |(value) => {}
| setValue | Function to set value and validate field | |(curentFieldConfig) => partialFieldConfig
| updateConfig | Function to partially update a fields config | |() => {}
| validate | Function to validate the field | |(value) => {}
| setValueOnly | Function only set value without validating | |
| key | What it does? | Type | Example |
| ----------------- | :-----------------------------------------------------: | :-------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------: |
| isValid | Validity of the form | Boolean | |() => {}
| validate | Function to validate the form | | |fn(fieldName, fieldConfig)
| addFieldConfig | Dynamically add a field | or fn(fieldName, (formState)) => fieldConfig | formObject.addFieldConfig('useraname', { required: true }) or formObject.addFieldConfig('useraname', (formState) => { required: !formState.email }) |fn(fieldName)
| removeFieldConfig | Dynamically remove a field | | formObject.removeFieldConfig('useraname') |() => {}
| reset | Resets the form config before adding or removing fields | | |{}` |
| config | Current form config |