Top Related Projects
Build forms in React, without the tears 😭
📋 React Hooks for form state management and validation (Web + React Native)
🏁 High performance subscription-based form state management for React
Performance-focused API for React forms 🚀
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
A Higher Order Component using react-redux to keep form state in a Redux store
Quick Overview
Formsy-React is a form validation library for React applications. It provides a flexible and easy-to-use solution for handling form state, validation, and submission in React components. The library aims to simplify form management while offering powerful validation capabilities.
Pros
- Easy integration with React components
- Flexible validation rules and custom validators
- Supports both synchronous and asynchronous validation
- Provides a clean and declarative API for form management
Cons
- Limited built-in UI components (focuses mainly on logic)
- Learning curve for more complex validation scenarios
- Less active development in recent years
- Some users report issues with TypeScript support
Code Examples
- Basic form setup:
import { Formsy, Form, Input } from 'formsy-react';
const MyForm = () => (
<Formsy onValidSubmit={onSubmit}>
<Input name="email" validations="isEmail" validationError="Invalid email" required />
<button type="submit">Submit</button>
</Formsy>
);
- Custom validation rule:
import { Formsy, addValidationRule } from 'formsy-react';
addValidationRule('isEven', (values, value) => {
return parseInt(value) % 2 === 0;
});
const MyForm = () => (
<Formsy>
<Input name="number" validations="isEven" validationError="Must be even" />
</Formsy>
);
- Form-level validation:
import { Formsy, Form, Input } from 'formsy-react';
const MyForm = () => (
<Formsy
onValidSubmit={onSubmit}
validationErrors={{
matchPassword: 'Passwords do not match'
}}
>
<Input name="password" type="password" />
<Input name="passwordConfirm" type="password" validations="equalsField:password" />
</Formsy>
);
Getting Started
To use Formsy-React in your project, follow these steps:
-
Install the package:
npm install formsy-react
-
Import and use in your React component:
import React from 'react'; import { Formsy, Form, Input } from 'formsy-react'; const MyForm = () => { const onSubmit = (data) => { console.log('Form data:', data); }; return ( <Formsy onValidSubmit={onSubmit}> <Input name="name" validations="isAlpha" validationError="Name must be letters only" required /> <Input name="email" validations="isEmail" validationError="Invalid email" required /> <button type="submit">Submit</button> </Formsy> ); }; export default MyForm;
This setup creates a basic form with name and email fields, along with basic validation rules.
Competitor Comparisons
Build forms in React, without the tears 😭
Pros of Formik
- More active development and larger community support
- Built-in Yup integration for schema validation
- Better TypeScript support and type definitions
Cons of Formik
- Steeper learning curve for beginners
- More verbose setup for simple forms
- Larger bundle size compared to Formsy-React
Code Comparison
Formik:
import { Formik, Form, Field } from 'formik';
<Formik
initialValues={{ email: '', password: '' }}
onSubmit={(values) => console.log(values)}
>
<Form>
<Field name="email" type="email" />
<Field name="password" type="password" />
<button type="submit">Submit</button>
</Form>
</Formik>
Formsy-React:
import Formsy from 'formsy-react';
import { Input } from 'formsy-react-components';
<Formsy onSubmit={(values) => console.log(values)}>
<Input name="email" type="email" />
<Input name="password" type="password" />
<button type="submit">Submit</button>
</Formsy>
Formik offers more flexibility and features but requires more setup, while Formsy-React provides a simpler API for basic form handling. Formik's popularity and active development make it a more future-proof choice for complex form management in React applications.
📋 React Hooks for form state management and validation (Web + React Native)
Pros of react-hook-form
- Lightweight and performant, with minimal re-renders
- Built-in validation and error handling
- Seamless integration with TypeScript
Cons of react-hook-form
- Steeper learning curve for developers new to React Hooks
- Less opinionated, which may require more setup for complex forms
Code Comparison
formsy-react:
<Formsy onSubmit={this.submit}>
<MyInput name="email" validations="isEmail" validationError="Invalid email" required />
<button type="submit">Submit</button>
</Formsy>
react-hook-form:
const { register, handleSubmit, errors } = useForm();
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input name="email" ref={register({ required: true, pattern: /^\S+@\S+$/i })} />
{errors.email && <span>This field is required</span>}
<button type="submit">Submit</button>
</form>
);
react-hook-form offers a more modern approach using hooks, while formsy-react uses a higher-order component pattern. react-hook-form provides more flexibility and control over form state and validation, but may require more setup. formsy-react offers a simpler API for basic forms but may be less flexible for complex scenarios.
🏁 High performance subscription-based form state management for React
Pros of react-final-form
- More flexible and customizable, allowing for complex form structures
- Better performance with large forms due to optimized rendering
- Supports asynchronous validation and submission out of the box
Cons of react-final-form
- Steeper learning curve, especially for beginners
- Requires more boilerplate code for basic forms
- Less opinionated, which may lead to inconsistent implementations across projects
Code Comparison
formsy-react:
<Formsy onSubmit={this.submit}>
<MyInput name="email" validations="isEmail" validationError="Invalid email" required />
<button type="submit">Submit</button>
</Formsy>
react-final-form:
<Form
onSubmit={onSubmit}
render={({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field name="email" validate={isEmail}>
{({ input, meta }) => (
<div>
<input {...input} type="email" placeholder="Email" />
{meta.error && meta.touched && <span>{meta.error}</span>}
</div>
)}
</Field>
<button type="submit">Submit</button>
</form>
)}
/>
Performance-focused API for React forms 🚀
Pros of unform
- More modern and actively maintained, with recent updates and releases
- Built with TypeScript, offering better type safety and developer experience
- Supports React Native out of the box, providing a unified solution for web and mobile
Cons of unform
- Steeper learning curve due to its more complex API and concepts
- Less extensive documentation compared to formsy-react
- Smaller community and fewer third-party integrations
Code Comparison
unform:
import { Form } from '@unform/web';
import Input from './components/Input';
function MyForm() {
const handleSubmit = (data) => {
console.log(data);
};
return (
<Form onSubmit={handleSubmit}>
<Input name="email" />
<button type="submit">Submit</button>
</Form>
);
}
formsy-react:
import Formsy from 'formsy-react';
import MyInput from './MyInput';
function MyForm() {
const handleSubmit = (model) => {
console.log(model);
};
return (
<Formsy onSubmit={handleSubmit}>
<MyInput name="email" validations="isEmail" />
<button type="submit">Submit</button>
</Formsy>
);
}
The code comparison shows that unform uses a more modern approach with custom components, while formsy-react relies on higher-order components and predefined validations. unform's API is more flexible but requires more setup, whereas formsy-react offers a simpler, more opinionated structure.
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
Pros of TanStack Form
- More modern and actively maintained, with regular updates and improvements
- Offers a flexible, headless approach that works with any UI library or framework
- Provides advanced features like form state management and field array handling
Cons of TanStack Form
- Steeper learning curve due to its more complex API and concepts
- Requires more setup and configuration compared to Formsy-React's simpler approach
- May be overkill for basic form handling needs
Code Comparison
Formsy-React:
import { withFormsy } from 'formsy-react';
const MyInput = withFormsy(({ setValue, getValue, errorMessage }) => (
<div>
<input onChange={e => setValue(e.target.value)} value={getValue() || ''} />
<span>{errorMessage}</span>
</div>
));
TanStack Form:
import { useForm } from '@tanstack/react-form';
const MyForm = () => {
const form = useForm({
defaultValues: { name: '' },
onSubmit: values => console.log(values),
});
return (
<form.Field name="name">
{field => <input {...field.getInputProps()} />}
</form.Field>
);
};
A Higher Order Component using react-redux to keep form state in a Redux store
Pros of redux-form
- Seamless integration with Redux, allowing for centralized state management
- Robust validation and error handling capabilities
- Extensive documentation and community support
Cons of redux-form
- Steeper learning curve, especially for developers new to Redux
- Can be overkill for simple forms or projects not using Redux
- Performance concerns with large forms due to frequent re-renders
Code Comparison
redux-form:
import { reduxForm, Field } from 'redux-form';
const MyForm = ({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field name="username" component="input" type="text" />
<button type="submit">Submit</button>
</form>
);
export default reduxForm({ form: 'myForm' })(MyForm);
formsy-react:
import { Formsy, Form, Input } from 'formsy-react';
const MyForm = () => (
<Formsy onSubmit={handleSubmit}>
<Input name="username" type="text" />
<button type="submit">Submit</button>
</Formsy>
);
export default MyForm;
Both libraries provide form handling solutions for React applications, but redux-form is more tightly coupled with Redux and offers more advanced features. formsy-react, on the other hand, is simpler to set up and use, making it a good choice for projects that don't require complex form management or Redux integration.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Moved!
This project has moved. Starting from 1.0.0 onward, develeopment will continue at https://github.com/formsy/formsy-react/
formsy-react
A form input builder and validator for React JS
How to use | API | Examples |
---|
Background
I wrote an article on forms and validation with React JS, Nailing that validation with React JS, the result of that was this extension.
The main concept is that forms, inputs and validation is done very differently across developers and projects. This extension to React JS aims to be that "sweet spot" between flexibility and reusability.
What you can do
-
Build any kind of form element components. Not just traditional inputs, but anything you want and get that validation for free
-
Add validation rules and use them with simple syntax
-
Use handlers for different states of your form. Ex. "onSubmit", "onError", "onValid" etc.
-
Pass external errors to the form to invalidate elements
-
You can dynamically add form elements to your form and they will register/unregister to the form
Default elements
You can look at examples in this repo or use the formsy-react-components project to use bootstrap with formsy-react, or use formsy-material-ui to use Material-UI with formsy-react.
Install
- Download from this REPO and use globally (Formsy) or with requirejs
- Install with
npm install formsy-react
and use with browserify etc. - Install with
bower install formsy-react
Changes
How to use
See examples
folder for examples. Codepen demo.
Complete API reference is available here.
Formsy gives you a form straight out of the box
import Formsy from 'formsy-react';
const MyAppForm = React.createClass({
getInitialState() {
return {
canSubmit: false
}
},
enableButton() {
this.setState({
canSubmit: true
});
},
disableButton() {
this.setState({
canSubmit: false
});
},
submit(model) {
someDep.saveEmail(model.email);
},
render() {
return (
<Formsy.Form onValidSubmit={this.submit} onValid={this.enableButton} onInvalid={this.disableButton}>
<MyOwnInput name="email" validations="isEmail" validationError="This is not a valid email" required/>
<button type="submit" disabled={!this.state.canSubmit}>Submit</button>
</Formsy.Form>
);
}
});
This code results in a form with a submit button that will run the submit
method when the submit button is clicked with a valid email. The submit button is disabled as long as the input is empty (required) or the value is not an email (isEmail). On validation error it will show the message: "This is not a valid email".
Building a form element (required)
import Formsy from 'formsy-react';
const MyOwnInput = React.createClass({
// Add the Formsy Mixin
mixins: [Formsy.Mixin],
// setValue() will set the value of the component, which in
// turn will validate it and the rest of the form
changeValue(event) {
this.setValue(event.currentTarget.value);
},
render() {
// Set a specific className based on the validation
// state of this component. showRequired() is true
// when the value is empty and the required prop is
// passed to the input. showError() is true when the
// value typed is invalid
const className = this.showRequired() ? 'required' : this.showError() ? 'error' : null;
// An error message is returned ONLY if the component is invalid
// or the server has returned an error message
const errorMessage = this.getErrorMessage();
return (
<div className={className}>
<input type="text" onChange={this.changeValue} value={this.getValue()}/>
<span>{errorMessage}</span>
</div>
);
}
});
The form element component is what gives the form validation functionality to whatever you want to put inside this wrapper. You do not have to use traditional inputs, it can be anything you want and the value of the form element can also be anything you want. As you can see it is very flexible, you just have a small API to help you identify the state of the component and set its value.
Related projects
- formsy-material-ui - A formsy-react compatibility wrapper for Material-UI form components.
- formsy-react-components - A set of React JS components for use in a formsy-react form.
- ...
- Send PR for adding your project to this list!
Contribute
- Fork repo
npm install
npm run examples
runs the development server onlocalhost:8080
npm test
runs the tests
License
Copyright (c) 2014-2016 PatientSky A/S
Top Related Projects
Build forms in React, without the tears 😭
📋 React Hooks for form state management and validation (Web + React Native)
🏁 High performance subscription-based form state management for React
Performance-focused API for React forms 🚀
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
A Higher Order Component using react-redux to keep form state in a Redux store
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot