react-field-validation-form
Custom React Hook for managing field and form level validation using Yup with support for nested data, arrays and async values.
Install
npm install --save react-field-validation-form
Usage
Basic
import React useState from 'react'import string from 'yup'import useFieldValidationForm from 'react-field-validation-form'import Button Box FormLabel Input from '@chakra-ui/core' { const values setValues = const formData errors: formErrors handleChange handleSubmit handleValidateField } = return <Box> <Box ='8' /> <form ='POST' => <FormLabel ='email'>Email</FormLabel> <Input ='text' ='email' ='email' = = = /> <div>formErrors?email}</div> <Box ='8' /> <FormLabel ='password'>Password</FormLabel> <Input ='password' ='password' ='password' = = = /> <div>formErrors?password}</div> <Box ='8' /> <Button ='solid' ='submit'> submit' ' </Button> </form> <Box ='8' /> <Box>Output Values: values && JSON</Box> </Box> )
Nested Object
import React useState from 'react'import string from 'yup'import useFieldValidationForm from 'react-field-validation-form'import Button Box FormLabel Input from '@chakra-ui/core' { const values setValues = const formData errors: formErrors handleChange handleSubmit handleValidateField } = return <Box> <Box ='8' /> <form ='POST' => <FormLabel ='name'>Name</FormLabel> <Input ='name' ='user.name' ='name' = = = /> <div>formErrors?user?name}</div> <Box ='8' /> <FormLabel ='email'>Email</FormLabel> <Input ='text' ='user.email' ='email' = = = /> <div>formErrors?user?email}</div> <Box ='8' /> <Button ='solid' ='submit'> submit' ' </Button> </form> <Box ='8' /> <Box>Output Values: values && JSON</Box> </Box> )}
With Array
import React useState from 'react'import string from 'yup'import useFieldValidationForm from 'react-field-validation-form'import Button Box FormLabel Input IconButton Flex from '@chakra-ui/core'import generate from 'shortid' { const values setValues = const formData errors: formErrors handleChange handleSubmit handleValidateField handleArrayOnChange handleValidateArrayField handleArrayRemoveField handleArrayPushField } = return <Box> <Box ='8' /> <form ='POST' => <FormLabel ='name'>Name</FormLabel> <Input ='name' ='name' ='name' = = = /> <div>formErrors?name}</div> <Box ='8' /> formDatafood
With Async Data
import React useState useEffect from 'react'import string from 'yup'import useFieldValidationForm from 'react-field-validation-form'import Button Box FormLabel Input IconButton Flex from '@chakra-ui/core'import generate from 'shortid' { const asyncData setAsyncData = const isLoading setLoading = const values setValues = const formData errors: formErrors handleChange handleSubmit handleValidateField handleArrayOnChange handleValidateArrayField handleArrayRemoveField handleArrayPushField } =
Check examples folder for in depth usage.
See them in action
OrLicense
MIT © VictorBaba