formsy-react ============ A form input builder and validator for React JS - [Background](#background) - [What you can do](#whatyoucando) - [Install](#install) - [Changes](#changes) - [How to use](#howtouse) - [API](#API) - [Formsy.defaults](#formsydefaults) - [Formsy.Form](#formsyform) - [className](#classname) - [url](#url) - [method](#method) - [contentType](#contenttype) - [hideSubmit](#hideSubmit) - [submitButtonClass](#submitButtonClass) - [cancelButtonClass](#cancelButtonClass) - [buttonWrapperClass](#buttonWrapperClass) - [onSuccess()](#onsuccess) - [onSubmit()](#onsubmit) - [onSubmitted()](#onsubmitted) - [onCancel()](#oncancel) - [onError()](#onerror) - [Formsy.Mixin](#formsymixin) - [name](#name) - [validations](#validations) - [validationError](#validationerror) - [required](#required) - [getValue()](#getvalue) - [setValue()](#setvalue) - [hasValue()](#hasvalue) - [resetValue()](#resetvalue) - [getErrorMessage()](#geterrormessage) - [isValid()](#isvalid) - [isRequired()](#isrequired) - [showRequired()](#showrequired) - [showError()](#showerror) - [Formsy.addValidationRule](#formsyaddvalidationrule) - [Validators](#validators) ## Background I wrote an article on forms and validation with React JS, [Nailing that validation with React JS](http://christianalfoni.github.io/javascript/2014/10/22/nailing-that-validation-with-reactjs.html), 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 1. Build any kind of form input components. Not just traditional inputs, but anything you want and get that validation for free 2. Add validation rules and use them with simple syntax 3. Use handlers for different states of your form. Ex. "onSubmit", "onError" etc. 4. Server validation errors automatically binds to the correct form input component ## Install 1. Download from this REPO and use globally (Formsy) or with requirejs 2. Install with `npm install formsy-react` and use with browserify etc. 3. Install with `bower install formsy-react` ## Changes **0.2.2**: - Fixed bug with updating the props **0.2.1**: - Cancel button displays if onCancel handler is defined **0.2.0**: - Implemented hasValue() method **0.1.3**: - Fixed resetValue bug **0.1.2**: - Fixed isValue check to empty string, needs to support false and 0 **0.1.1**: - Added resetValue method - Changed value check of showRequired ## How to use #### Formsy gives you a form straight out of the box ```javascript /** @jsx React.DOM */ var Formsy = require('formsy-react'); var MyAppForm = React.createClass({ changeUrl: function () { location.href = '/success'; }, render: function () { return ( ); } }); ``` This code results in a form with a submit button that will POST to /users when clicked. 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". #### This is an example of what you can enjoy building ```javascript /** @jsx React.DOM */ var Formsy = require('formsy-react'); var 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: function (event) { this.setValue(event.currentTarget.value); }, render: function () { // 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 var 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 var errorMessage = this.getErrorMessage(); return (
{errorMessage}
); } }); ``` So this is basically how you build your form elements. 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. ## API ### Formsy.defaults(options) ```javascript Formsy.defaults({ contentType: 'urlencoded', // default: 'json' hideSubmit: true, // default: false submitButtonClass: 'btn btn-success', // default: null cancelButtonClass: 'btn btn-default', // default: null buttonWrapperClass: 'my-wrapper' // default: null }); ``` Use **defaults** to set general settings for all your forms. ### Formsy.Form #### className ```html ``` Sets a class name on the form itself. #### url ```html ``` Will either **POST** or **PUT** to the url specified when submitted. #### method ```html ``` Supports **POST** (default) and **PUT**. #### contentType ```html ``` Supports **json** (default) and **urlencoded** (x-www-form-urlencoded). **Note!** Response has to be **json**. #### hideSubmit ```html ``` Hides the submit button. Submit is done by ENTER on an input. #### submitButtonClass ```html ``` Sets a class name on the submit button. #### cancelButtonClass ```html ``` Sets a class name on the cancel button. #### buttonWrapperClass ```html ``` Sets a class name on the container that wraps the **submit** and **cancel** buttons. #### onSuccess(serverResponse) ```html ``` Takes a function to run when the server has responded with a success http status code. #### onSubmit() ```html ``` Takes a function to run when the submit button has been clicked. #### onSubmitted() ```html ``` Takes a function to run when either a success or error response is received from the server. #### onCancel() ```html ``` Will display a "cancel" button next to submit. On click it runs the function handler. #### onError(serverResponse) ```html ``` Takes a function to run when the server responds with an error http status code. ### Formsy.Mixin #### name ```html ``` The name is required to register the form input component in the form. #### validations ```html ``` An comma seperated list with validation rules. Take a look at **Validators** to see default rules. Use ":" to separate arguments passed to the validator. The arguments will go through a **JSON.parse** converting them into correct JavaScript types. Meaning: ```html ``` Works just fine. #### validationError ```html ``` The message that will show when the form input component is invalid. #### required ```html ``` A property that tells the form that the form input component value is required. #### getValue() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], render: function () { return ( ); } }); ``` Gets the current value of the form input component. #### setValue(value) ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return ( ); } }); ``` Sets the value of your form input component. Notice that it does not have to be a text input. Anything can set a value on the component. Think calendars, checkboxes, autocomplete stuff etc. #### hasValue() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
{this.hasValue() ? 'There is a value here' : 'No value entered yet'}
); } }); ``` The hasValue() method helps you identify if there actually is a value or not. The only invalid value in Formsy is an empty string, "". All other values are valid as they could be something you want to send to the server. F.ex. the number zero (0), or false. #### resetValue() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
); } }); ``` Resets to empty value. #### getErrorMessage() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
{this.getErrorMessage}
); } }); ``` Will return the server error mapped to the form input component or return the validation message set if the form input component is invalid. If no server error and form input component is valid it returns **null**. #### isValid() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { var face = this.isValid() ? ':-)' : ':-('; return (
{face} {this.getErrorMessage}
); } }); ``` Returns the valid state of the form input component. #### isRequired() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
{this.props.label} {this.isRequired() ? '*' : null} {this.getErrorMessage}
); } }); ``` Returns true if the required property has been passed. #### showRequired() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { var className = this.showRequired() ? 'required' : ''; return (
{this.getErrorMessage}
); } }); ``` Lets you check if the form input component should indicate if it is a required field. This happens when the form input component value is empty and the required prop has been passed. #### showError() ```javascript var MyInput = React.createClass({ changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { var className = this.showRequired() ? 'required' : this.showError() ? 'error' : ''; return (
{this.getErrorMessage}
); } }); ``` Lets you check if the form input component should indicate if there is an error. This happens if there is a form input component value and it is invalid or if a server error is received. ### Formsy.addValidationRule(name, ruleFunc) An example: ```javascript Formsy.addValidationRule('isFruit', function (value) { return ['apple', 'orange', 'pear'].indexOf(value) >= 0; }); ``` ```html ``` Another example: ```javascript Formsy.addValidationRule('isIn', function (value, array) { return array.indexOf(value) >= 0; }); ``` ```html ``` ## Validators **isValue** ```html ``` Returns true if the value is thruthful **isEmail** ```html ``` Return true if it is an email **isTrue** ```html ``` Returns true if the value is the boolean true **isNumeric** ```html ``` Returns true if string only contains numbers **isAlpha** ```html ``` Returns true if string is only letters **isLength:min**, **isLength:min:max** ```html ``` Returns true if the value length is the equal or more than minimum and equal or less than maximum, if maximum is passed **equals:value** ```html ``` Return true if the value from input component matches value passed (==).