# API - [Formsy.defaults](#formsydefaults) - [Formsy.Form](#formsyform) - [className](#classname) - [mapping](#mapping) - [validationErrors](#validationerrors) - [onSubmit()](#onsubmit) - [onValid()](#onvalid) - [onInvalid()](#oninvalid) - [onValidSubmit()](#onvalidsubmit) - [onInvalidSubmit()](#oninvalidsubmit) - [onChange()](#onchange) - [reset()](#resetform) - [Formsy.Mixin](#formsymixin) - [name](#name) - [value](#value) - [validations](#validations) - [validationError](#validationerror) - [validationErrors](#validationerrors) - [required](#required) - [getValue()](#getvalue) - [setValue()](#setvalue) - [hasValue() - DEPRECATED](#hasvalue) - [resetValue()](#resetvalue) - [getErrorMessage()](#geterrormessage) - [isValid()](#isvalid) - [isValidValue()](#isvalidvalue) - [isRequired()](#isrequired) - [showRequired()](#showrequired) - [showError()](#showerror) - [isPristine()](#ispristine) - [isFormDisabled()](#isformdisabled) - [isFormSubmitted()](#isFormSubmitted) - [validate](#validate) - [formNoValidate](#formnovalidate) - [Formsy.addValidationRule](#formsyaddvalidationrule) - [Validators](#validators) ### Formsy.defaults(options) - DEPRECATED ### Formsy.Form #### className ```html ``` Sets a class name on the form itself. #### mapping ```javascript var MyForm = React.createClass({ mapInputs: function (inputs) { return { 'field1': inputs.foo, 'field2': inputs.bar }; }, submit: function (model) { model; // {field1: '', field2: ''} }, render: function () { return ( ); } }) ``` Use mapping to change the data structure of your input elements. This structure is passed to the submit hooks. #### validationErrors You can manually pass down errors to your form. In combination with `onChange` you are able to validate using an external validator. ```js var Form = React.createClass({ getInitialState: function () { return { validationErrors: {} }; }, validateForm: function (values) { if (!values.foo) { this.setState({ validationErrors: { foo: 'Has no value' } }); } else { this.setState({ validationErrors: {} }); } }, render: function () { return ( ); } }); ``` #### onSubmit(data, resetForm, invalidateForm) ```html ``` Takes a function to run when the submit button has been clicked. The first argument is the data of the form. The second argument will reset the form. The third argument will invalidate the form by taking an object that maps to inputs. E.g. `{email: "This email is taken"}`. Resetting or invalidating the form will cause **setState** to run on the form element component. #### onValid() ```html ``` Whenever the form becomes valid the "onValid" handler is called. Use it to change state of buttons or whatever your heart desires. #### onInvalid() ```html ``` Whenever the form becomes invalid the "onInvalid" handler is called. Use it to for example revert "onValid" state. #### onValidSubmit(model, resetForm, invalidateForm) ```html ``` Triggers when form is submitted with a valid state. The arguments are the same as on `onSubmit`. #### onInvalidSubmit(model, resetForm, invalidateForm) ```html ``` Triggers when form is submitted with an invalid state. The arguments are the same as on `onSubmit`. #### onChange(currentValues) ```html ``` "onChange" triggers when setValue is called on your form elements. It is also triggered when dynamic form elements have been added to the form. The "currentValues" is an object where the key is the name of the input and the value is the current value. #### reset() ```html var MyForm = React.createClass({ resetForm: function () { this.refs.form.reset(); }, render: function () { return ( ... ); } }); ``` Manually reset the form to its pristine state. ### Formsy.Mixin #### name ```html ``` The name is required to register the form input component in the form. #### value ```html ``` You should always use the [**getValue()**](#getvalue) method inside your formsy form element. To pass an initial value, use the value attribute. This value will become the "pristine" value and any reset of the form will bring back this value. #### validations ```html ``` An comma seperated list with validation rules. Take a look at [**Validators**](#validators) to see default rules. Use ":" to separate argument passed to the validator. The argument 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. It will be used as a default error. #### validationErrors ```html ``` The message that will show when the form input component is invalid. You can combine this with `validationError`. Keys not found in `validationErrors` defaults to the general error message. #### required ```html ``` A property that tells the form that the form input component value is required. By default it uses `isDefaultRequiredValue`, but you can define your own definition of what defined a required state. ```html ``` Would be typical for a checkbox type of form element. #### 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({ mixins: [Formsy.Mixin], 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. Running this method will trigger a **setState()** on the component and do a render. #### hasValue() - DEPRECATED ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], 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({ mixins: [Formsy.Mixin], changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
); } }); ``` Resets to empty value. This will run a **setState()** on the component and do a render. #### getErrorMessage() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
{this.getErrorMessage()}
); } }); ``` Will return the validation message set if the form input component is invalid. If form input component is valid it returns **null**. #### isValid() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], 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. #### isValidValue() You can pre-verify a value against the passed validators to the form element. ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], changeValue: function (event) { if (this.isValidValue(event.target.value)) { this.setValue(event.target.value); } }, render: function () { return ; } }); var MyForm = React.createClass({ render: function () { return ( ); } }); ``` #### isRequired() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], 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({ mixins: [Formsy.Mixin], 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({ mixins: [Formsy.Mixin], 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. #### isPristine() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], changeValue: function (event) { this.setValue(event.currentTarget.value); }, render: function () { return (
{this.isPristine() ? 'You have not touched this yet' : ''}
); } }); ``` By default all formsy input elements are pristine, which means they are not "touched". As soon as the [**setValue**](#setvaluevalue) method is run it will no longer be pristine. **note!** When the form is reset, using the resetForm callback function on for example [**onSubmit**](#onsubmitdata-resetform-invalidateform) the inputs are reset to their pristine state. #### isFormDisabled() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], render: function () { return (
); } }); React.render(); ``` You can now disable the form itself with a prop and use **isFormDisabled()** inside form elements to verify this prop. #### isFormSubmitted() ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], render: function () { var error = this.isFormSubmitted() ? this.getErrorMessage() : null; return (
{error}
); } }); ``` You can check if the form has been submitted. #### validate ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], changeValue: function (event) { this.setValue(event.target.value); }, validate: function () { return !!this.getValue(); }, render: function () { return (
); } }); React.render(); ``` You can create custom validation inside a form element. The validate method defined will be run when you set new values to the form element. It will also be run when the form validates itself. This is an alternative to passing in validation rules as props. #### formNoValidate To avoid native validation behavior on inputs, use the React `formNoValidate` property. ```javascript var MyInput = React.createClass({ mixins: [Formsy.Mixin], render: function () { return (
); } }); ``` ### Formsy.addValidationRule(name, ruleFunc) An example: ```javascript Formsy.addValidationRule('isFruit', function (values, value) { return ['apple', 'orange', 'pear'].indexOf(value) >= 0; }); ``` ```html ``` Another example: ```javascript Formsy.addValidationRule('isIn', function (values, value, array) { return array.indexOf(value) >= 0; }); ``` ```html ``` Cross input validation: ```javascript Formsy.addValidationRule('isMoreThan', function (values, value, otherField) { // The this context points to an object containing the values // {childAge: "", parentAge: "5"} // otherField argument is from the validations rule ("childAge") return Number(value) > Number(values[otherField]); }); ``` ```html ``` ## Validators **matchRegexp** ```html ``` Returns true if the value is thruthful **isEmail** ```html ``` Return true if it is an email **isUndefined** ```html ``` Returns true if the value is the undefined **isEmptyString** ```html ``` Returns true if the value is an empty string **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 **isWords** ```html ``` Returns true if string is only letters, including spaces and tabs **isSpecialWords** ```html ``` Returns true if string is only letters, including special letters (a-z,ú,ø,æ,å) **isLength** ```html ``` Returns true if the value length is the equal. **equals:value** ```html ``` Return true if the value from input component matches value passed (==). **equalsField:fieldName** ```html ``` Return true if the value from input component matches value passed (==). **minLength:length** ```html ``` Return true if the value is more or equal to argument **maxLength:length** ```html ``` Return true if the value is less or equal to argument