Socket
Socket
Sign inDemoInstall

use-validate-form

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-validate-form

react hook for form validation


Version published
Weekly downloads
3
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

use-validate-form

This package exposes useFormValidation, a custom react hook for validating form input.

Usage

You can import and use useValidateForm like this:

import {
  useValidateForm,
  isRequired
} from 'use-validate-form'

const fields = {
  name: {initialValue: '', validators: [isRequired]}
  age: {initialValue: '', validators: [isRequired]}
}

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
  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

Package last updated on 29 May 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc