Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
use-validate-form
Advanced tools
This package exposes useFormValidation
, a custom react hook for validating form input.
You can import and use useValidateForm
like this:
import {
useValidateForm,
isRequired
} from 'use-validate-form'
const fields = {
name: {initialValue: '', validators: [isRequired], type: 'string'}
age: {initialValue: '', validators: [isRequired], type: 'number'}
}
const [
{
fields: { name, age },
isValid,
hasErrors,
},
dispatch
] = useValidateForm(fields)
Each item in the validators
arrays must be an objects with a func
property which returns true
or false
and an error
property which is a string that will explain to a user why their input is invalid.
Each of the field objects returned by useValidateForm
(i.e. name
, age
) have the following properties:
const {
value, // value of field
type, // string
isValid, // boolean
isDirty, // boolean
errors, // array of strings
name, // string
validators // array of functions
} = age
The dispatch
function takes an action of type SET_VALUE
or VALIDATE
. This allows you to set the value of a field onChange
event but only validate (show errors) onBlur
.
<input
onChange={(event) => dispatch({type: 'SET_VALUE', {name: 'age', value: event.target.value}})}
onBlur={(event) => dispatch({type: 'VALIDATE', {name: 'age', value: event.target.value}})}
/>
The name
property of the action must match the name
property from the field which was originally passed into useValidateForm
.
FAQs
react hook for form validation
The npm package use-validate-form receives a total of 3 weekly downloads. As such, use-validate-form popularity was classified as not popular.
We found that use-validate-form demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.