Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
React Hook Form | 35,124 | 127 | 1,337 | 7 hours ago | 951 | September 23, 2022 | 14 | mit | TypeScript | |
📋 React Hooks for form state management and validation (Web + React Native) | ||||||||||
Formik | 32,396 | 2,455 | 1,807 | 6 hours ago | 201 | June 02, 2021 | 769 | apache-2.0 | TypeScript | |
Build forms in React, without the tears 😭 | ||||||||||
React Jsonschema Form | 12,700 | 566 | 281 | 15 hours ago | 103 | December 16, 2019 | 291 | apache-2.0 | TypeScript | |
A React component for building Web forms from JSON Schema. | ||||||||||
Redux Form | 12,611 | 11,781 | 1,243 | 2 months ago | 235 | November 17, 2021 | 491 | mit | JavaScript | |
A Higher Order Component using react-redux to keep form state in a Redux store | ||||||||||
Formily | 9,628 | 119 | 3 days ago | 209 | September 20, 2022 | 12 | mit | TypeScript | ||
📱🚀 🧩 Cross Device & High Performance Normal Form/Dynamic(JSON Schema) Form/Form Builder -- Support React/React Native/Vue 2/Vue 3 | ||||||||||
React Final Form | 7,257 | 500 | 384 | 12 days ago | 76 | April 01, 2022 | 390 | mit | JavaScript | |
🏁 High performance subscription-based form state management for React | ||||||||||
X Render | 6,070 | 1 | 31 | 12 hours ago | 310 | September 21, 2022 | 13 | TypeScript | ||
🚴♀️ 阿里 - 很易用的中后台「表单 / 表格 / 图表」解决方案 | ||||||||||
Unform | 4,514 | 33 | 2 months ago | 16 | February 19, 2021 | 73 | mit | TypeScript | ||
Performance-focused API for React forms 🚀 | ||||||||||
Survey Library | 3,615 | 37 | 15 | 8 hours ago | 384 | September 20, 2022 | 490 | mit | TypeScript | |
Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout. | ||||||||||
Form | 2,729 | 2 days ago | 29 | mit | TypeScript | |||||
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Svelte Form and Vue Form. |
Easy-to-use React hook for validating forms with the class-validator library.
npm install --save react-class-validator
const validatorOptions: ValidatorContextOptions = {
onErrorMessage: (error): string => {
// custom error message handling (localization, etc)
},
resultType: 'boolean' // default, can also be set to 'map'
}
render((
<ValidatorProvider options={validatorOptions}>
<MyComponent />
</ValidatorProvider>
), document.getElementById('root'))
The default behavior is to flatten all error constraints for each attribute.
const getDefaultContextOptions = (): ValidatorContextOptions => ({
onErrorMessage: (error) => Object.keys(error.constraints).map((key) => error.constraints[key])
});
When using libraries such as react-intl, you don't have to modify the existing
onErrorMessage
handler. Decorators are handled at source load, so you only need to include the intl.formatMessage
in your message definition.
class Person {
@IsEmail({}, {
message: intl.formatMessage(defineMessage({defaultMessage: 'Invalid email'}))
})
@IsNotEmpty({
message: intl.formatMessage(defineMessage({defaultMessage: 'Email cannot be empty'}))
})
public email: string;
}
Create a class using validation decorators from class-validator
.
import { IsNotEmpty } from "class-validator";
class LoginValidation {
@IsNotEmpty({
message: 'username cannot be empty'
})
public username: string;
@IsNotEmpty({
message: 'password cannot be empty'
})
public password: string;
}
Set up your form component to validate using your validation class.
const MyComponent = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [validate, errors] = useValidation(LoginValidation);
return (
<form onSubmit={async (evt) => {
evt.preventDefault();
// `validate` will return true if the submission is valid
if (await validate({username, password})) {
// ... handle valid submission
}
}}>
{/* use a filter so that the onBlur function will only validate username */}
<input value={username} onChange={({target: {value}}) => setUsername(value)}
onBlur={() => validate({username}, ['username'])}/>
{/* show error */}
{errors.username && (
<div className="error">
{errors.username.map((message) => <strong>message</strong>)}
</div>
)}
</form>
);
};
react-class-validator
easily integrates with Formik. You can simply use the validate
function returned from useValidation
, so long as the Formik fields are named the same as the keys in your validation
class. Individual fields will have to be validated with onBlur
functionality.
To display error messages without custom handling, messages will need to be outputted as a map upon validation.
Do this by overriding the default resultType
(you can also do this at the component-level).
const options: ValidatorContextOptions = {
resultType: 'map'
};
Then you can simply integrate with the default Formik flow.
export const Login: FunctionComponent = () => {
const [validate] = useValidation(LoginValidation);
return (
<Formik initialValues={{username: '', password: ''}}
validateOnBlur
validateOnChange
validate={validate}>
{({values, touched, errors, handleChange, handleBlur}) => (
<Form>
<label htmlFor="username">Username</label>
<Field id="username" name="username" placeholder="Username" />
{errors.username && touched.username ? (
<div>{errors.username}</div>
) : null}
{/* other fields */}
<button type="submit">
Submit
</button>
</Form>
)}
</Formik>
);
};
Library built and maintained by Robin Schultz
If you would like to contribute (aka buy me a beer), you can send funds via PayPal at the link below.