formsy-react-2
Formsy React 2 is a form input builder and validator for React JS. It is the successor to the formsy-react libary that was forked to add support for:
- React 15.5.x
- ES6 classes
- New validation rules
- Variable substitution in validation errors
For help migrating from formsy-react to formsy-react-2 see the Migrating from formsy-react section.
Installation
To get started with formsy-react-2, you can simply install it with npm:
npm i --save formsy-react-2
or with yarn
yarn add formsy-react-2
formsy-react-2 is currently compatible with React 15.5x. For React 15.4.x and below it is recommended that you install formsy-react instead.
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
Complete API reference is available here.
Basic Usage
import Formsy from 'formsy-react-2'; Mixin static defaultProps = type: 'text' { this; } { const type ...rest = this; const errorMessage = this; return <input = = = /> <span>errorMessage</span> } Component { thisprops; } { const errorMessage = this; return <input ='text' = = /> <span>errorMessage</span> } ; // Using your new component Component state = formIsValid: false { this; } { this; } { console; // model = { // foo: 'foo@foo.com', // bar: 10 // } } // 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 foo input is empty or the value is not an email; and // - the bar input is not an integer. // On validation error it will show the error message. { <Formsy.Form = = => <MyInput ='foo' ='isEmail' ='This is not a valid email' /> <MyInputHOC ='bar' ='isInt' 'This is not an integer' /> <button ="submit" =>Submit</button> </Formsy.Form> }
Migrating from formsy-react
Formsy.Mixin is no longer a mixin. It is now an ES6 class. To use it just create a new class that extends it. See the Basic Usage section for an example.
Eventually Mixin will be renamed to something that makes more sense but for now for backwards compatibility I have left it named as such.
The examples and API docs have not been updated yet.
If the original formsy-react maintainers become more active and incorporate these features then I will gladly remove this package.
For examples you can look at my other project formsy-mui
Contributing
- Fork repo
npm install
npm run build
npm test
runs the tests