Easy to use, and light weight library for form validations in React.
Before jumping to React, I was an Angular Developer. While I was learning React, I was like, "Oh! Angular kind of validating forms is missing in React", and the result is this library.
- Instead of importing a wrapper component for each field, add validations to the traditional HTML fields itself.
- Basic validators like
required
,email
,maxLength
,minLength
, andpattern
can be applied. - In addition to the basic validators, some most used validators like
validMobile
,validBirthDate
, andpasswordStrength
with customization are applied. - Changes uncontrolled inputs to controlled inputs internally, no need for explicitly chaining an
onchange
event. - Pass validation errors to the form to invalidate individual fields.
npm install react-formsio --save
import React from 'react';
import './App.css';
import { useFormsio } from 'react-formsio';
const App = () => {
const { register,
formState,
validationRules,
isFormValid } = useFormsio();
const { userName } = formState;
const handleSubmit = event => {
event.preventDefault();
}
return(
<div className = 'App'>
<form onSubmit = {handleSubmit} >
<input
type = 'text'
name = 'userName'
ref = { register({
validators: 'required|minLength:6'
}) }
autoComplete = 'off' />
{ userName?.errors?.required ?
<p> UserName is required </p> : null }
{ userName?.errors?.minLength ?
<p> UserName should be of minimum 6 character's length </p> : null }
<button type = 'submit'> Submit </button>
</form>
</div>
)
}
export default App;
See the API/Tutorial for more information.
If you find a bug or you'd like to request a new function, kindly open an issue here. Please include your queries and their expected results.
MIT © itzzmeakhi