RFV (React Form Validator)
React form validator and form handler.
RFV uses Validator.js as a validator engine, and Axios for HTTP requests.
Demo (Only form validator option)
Demo (Form validator and form handler option)
Installation
Install with Yarn.
$ yarn add rfv
Install with NPM.
$ npm i rfv
Usage
Only form validator option.
import React from 'react'import ReactDOM from 'react-dom' // Import packageimport Form Input Textarea from 'rfv' // Create validation rules (https://github.com/chriso/validator.js#validators)const validations = email: rule: 'isEmail' invalidFeedback: 'Please provide a valid email' message: rule: 'isLength' args: min: 1 invalidFeedback: 'Please provide a message' const App = // Learn the status of validation with `res.isFormValid` and get your form data as an object with `res.items` to make an AJAX request or something else const onSubmit = res if resisFormValid return // Build your form <Form => <div> <Input ='email' ='email' = /> </div> <div> <Textarea ='message' = /> </div> <div> <button>Submit</button> </div> </Form> ReactDOM
Form validator and form handler option.
import React from 'react'import ReactDOM from 'react-dom' // Import packageimport Form Input Textarea from 'rfv' // Create validation rules (https://github.com/chriso/validator.js#validators)const validations = email: rule: 'isEmail' invalidFeedback: 'Please provide a valid email' message: rule: 'isLength' args: min: 1 invalidFeedback: 'Please provide a message' const App = // After an AJAX call, call the `res.data` to get the backend results const postSubmit = res console return // Build your form <Form = => <div> <Input ='email' ='email' = /> </div> <div> <Textarea ='message' = /> </div> <div> <button>Submit</button> </div> </Form> ReactDOM
And add .is-invalid
and .invalid-feedback
classes into your CSS.
Make sure you add the errors to the validations
object in backend.
app
Props & Callbacks
<Form>
Props
// Since RFV uses Axios for HTTP requests, whatever you pass into postOptions prop except `data: {}`, directly goes into Axios<Form = =>
Callbacks
<Form = = =>
<Input>
, <Select>
, <Textarea>
Props
// You can pass more than one validation<Input = />
Callbacks
<Input = />
Contribution
Feel free to contribute. Open a new issue, or make a pull request.