diff --git a/.gitignore b/.gitignore
index f03556e..15d5043 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
.DS_Store
build
node_modules
+lib
diff --git a/.travis.yml b/.travis.yml
index e616d52..cc6db79 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,4 @@
language: node_js
node_js:
- - 'iojs'
- '0.12'
- - '0.11'
- '0.10'
diff --git a/API.md b/API.md
index 4d2bc81..fa1a198 100644
--- a/API.md
+++ b/API.md
@@ -35,6 +35,8 @@
- [isFormSubmitted()](#isformsubmitted)
- [validate](#validate)
- [formNoValidate](#formnovalidate)
+- [Formsy.HOC](#formsyhoc)
+- [Formsy.Decorator](#formsydecorator)
- [Formsy.addValidationRule](#formsyaddvalidationrule)
- [Validators](#validators)
@@ -109,7 +111,7 @@ var Form = React.createClass({
```html
```
-Takes a function to run when the submit button has been clicked.
+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. This is useful for server side validation. E.g. `{email: "This email is taken"}`. Resetting or invalidating the form will cause **setState** to run on the form element component.
@@ -226,12 +228,12 @@ The message that will show when the form input component is invalid. It will be
#### validationErrors
```html
-showRequired()
```javascript
@@ -424,7 +426,7 @@ var MyInput = React.createClass({
}
});
```
-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.
+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
@@ -539,6 +541,41 @@ var MyInput = React.createClass({
});
```
+### Formsy.HOC
+The same methods as the mixin are exposed to the HOC version of the element component, though through the `props`, not on the instance.
+```js
+import {HOC} from 'formsy-react';
+
+class MyInput extends React.Component {
+ render() {
+ return (
+
+ this.props.setValue(e.target.value)}/>
+
+ );
+ }
+};
+export default HOC(MyInput);
+```
+
+### Formsy.Decorator
+The same methods as the mixin are exposed to the decorator version of the element component, though through the `props`, not on the instance.
+```js
+import {Decorator as FormsyElement} from 'formsy-react';
+
+@FormsyElement()
+class MyInput extends React.Component {
+ render() {
+ return (
+
+ this.props.setValue(e.target.value)}/>
+
+ );
+ }
+};
+export default MyInput
+```
+
### Formsy.addValidationRule(name, ruleFunc)
An example:
```javascript
@@ -622,18 +659,36 @@ Returns true if the value is the boolean true
```
Returns true if the value is the boolean false
-**isNumeric**
-```html
-
-```
-Returns true if string only contains numbers
-
**isAlpha**
```html
```
Returns true if string is only letters
+**isNumeric**
+```html
+
+```
+Returns true if string only contains numbers. Examples: 42; -3.14
+
+**isAlphanumeric**
+```html
+
+```
+Returns true if string only contains letters or numbers
+
+**isInt**
+```html
+
+```
+Returns true if string represents integer value. Examples: 42; -12; 0
+
+**isFloat**
+```html
+
+```
+Returns true if string represents float value. Examples: 42; -3.14; 1e3
+
**isWords**
```html
diff --git a/README.md b/README.md
index 962916b..87b3c08 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ A form input builder and validator for React JS
### From version 0.12.0 Formsy only supports React 0.13.1 and up
## 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.
+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.
@@ -21,12 +21,15 @@ The main concept is that forms, inputs and validation is done very differently a
2. Add validation rules and use them with simple syntax
- 3. Use handlers for different states of your form. Ex. "onSubmit", "onError", "onValid" etc.
+ 3. Use handlers for different states of your form. Ex. "onSubmit", "onError", "onValid" etc.
- 4. Server validation errors automatically binds to the correct form input component
+ 4. Pass external errors to the form to invalidate elements
5. 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](https://github.com/twisty/formsy-react-components) project to use bootstrap with formsy-react.
+
## Install
1. Download from this REPO and use globally (Formsy) or with requirejs
@@ -91,7 +94,7 @@ This code results in a form with a submit button that will run the `submit` meth
// Add the Formsy Mixin
mixins: [Formsy.Mixin],
- // setValue() will set the value of the component, which in
+ // 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);
@@ -99,9 +102,9 @@ This code results in a form with a submit button that will run the `submit` meth
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
+ // 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;
diff --git a/bower.json b/bower.json
index a3eeaac..2c5602d 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "formsy-react",
- "version": "0.14.1",
+ "version": "0.16.0",
"description": "A form input builder and validator for React JS",
"repository": {
"type": "git",
diff --git a/build/test.js b/build/test.js
index e8908b0..39c8722 100644
--- a/build/test.js
+++ b/build/test.js
@@ -1,37 +1,55 @@
var React = require('react');
+var ReactDOM = require('react-dom');
var Formsy = require('./../src/main.js');
var Input = React.createClass({
-
- mixins: [Formsy.Mixin],
-
+ onChange: function (event) {
+ this.props.setValue(event.currentTarget.value);
+ },
render: function () {
return (
+ )
+ }
+});
+
+var FormApp = React.createClass({
onSubmit: function (model) {
console.log('model', model);
},
render: function () {
return (
-
-
+
+
);
}
});
-React.render(, document.getElementById('app'));
\ No newline at end of file
+ReactDOM.render(, document.getElementById('app'));
diff --git a/examples/README.md b/examples/README.md
index 1921954..691cc4f 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -29,4 +29,8 @@ If it is not helped try update your node.js and npm.
2. [**Custom Validation**](custom-validation)
- One field with added validation rule (`Formsy.addValidationRule`) and one field with dynamically added validation and error messages.
+ One field with added validation rule (`Formsy.addValidationRule`) and one field with dynamically added validation and error messages.
+
+3. [**Reset Values**](reset-values)
+
+ Reset text input, checkbox and select to their pristine values.
diff --git a/examples/components/Input.js b/examples/components/Input.js
new file mode 100644
index 0000000..a73e060
--- /dev/null
+++ b/examples/components/Input.js
@@ -0,0 +1,43 @@
+import React from 'react';
+import Formsy from 'formsy-react';
+
+const MyInput = 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[this.props.type === 'checkbox' ? 'checked' : '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.props.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 (
+