diff --git a/examples/custom-validation/app.css b/examples/custom-validation/app.css new file mode 100644 index 0000000..14985d7 --- /dev/null +++ b/examples/custom-validation/app.css @@ -0,0 +1,4 @@ +.custom-validation { + width: 500px; + margin: 0 auto; +} \ No newline at end of file diff --git a/examples/custom-validation/app.js b/examples/custom-validation/app.js new file mode 100644 index 0000000..9689938 --- /dev/null +++ b/examples/custom-validation/app.js @@ -0,0 +1,123 @@ +var React = require('react'); +var Formsy = require('./../..'); + +var currentYear = new Date().getFullYear(); + +var validators = { + time: { + regexp: /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/, + message: 'Not valid time' + }, + decimal: { + regexp: /(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\.\d*$)/, + message: 'Please type decimal value' + }, + binary: { + regexp: /^([0-1])*$/, + message: '10101000' + } +}; + +Formsy.addValidationRule('isYearOfBirth', function (value) { + value = parseInt(value); + if (typeof value !== 'number' || value !== value) { + return false; + } + return value < currentYear && value > currentYear - 130; +}); + +var App = React.createClass({ + submit: function (data) { + alert(JSON.stringify(data, null, 4)); + }, + render: function () { + return ( + + + + + + ); + } +}); + +var MyOwnInput = React.createClass({ + mixins: [Formsy.Mixin], + changeValue: function (event) { + this.setValue(event.currentTarget.value); + }, + render: function () { + var className = this.props.className + ' ' + (this.showError() ? 'error' : null); + var errorMessage = this.getErrorMessage(); + + return ( +
+ + + {errorMessage} +
+ ); + } +}); + +var DynamicInput = React.createClass({ + mixins: [Formsy.Mixin], + getInitialState: function() { + return { + validationType: 'time' + }; + }, + changeValue: function (event) { + this.setValue(event.currentTarget.value); + }, + changeValidation: function(validationType) { + this.setState({ + validationType: validationType + }); + this.setValue(this.getValue()); + }, + validate: function () { + var value = this.getValue(); + return value === '' ? true : validators[this.state.validationType].regexp.test(value); + }, + getCustomErrorMessage: function() { + return this.showError() ? validators[this.state.validationType].message : ''; + }, + render: function () { + var className = this.props.className + ' ' + (this.showError() ? 'error' : null); + var errorMessage = this.getCustomErrorMessage(); + + return ( +
+ + + {errorMessage} + +
+ ); + } +}); + +var Validations = React.createClass({ + changeValidation: function(e) { + this.props.changeValidation(e.target.value); + }, + render: function() { + return ( +
+ Validation Type +
+ Time +
+
+ Decimal +
+
+ Binary +
+
+ ); + } +}); + +React.render(, document.getElementById('example')); \ No newline at end of file diff --git a/examples/custom-validation/index.html b/examples/custom-validation/index.html new file mode 100644 index 0000000..1b28f43 --- /dev/null +++ b/examples/custom-validation/index.html @@ -0,0 +1,14 @@ + + + + Custom Validation Example + + + + +

Formsy React Examples / Custom Validation

+
+ + + + \ No newline at end of file diff --git a/examples/global.css b/examples/global.css index 5808257..15a2a75 100644 --- a/examples/global.css +++ b/examples/global.css @@ -25,17 +25,20 @@ form { } .form-group { - margin-bottom: 15px; + margin-bottom: 10px; } .form-group label { display: inline-block; max-width: 100%; - margin-bottom: 5px; + margin-top: 5px; font-weight: 700; } -.form-group input { +.form-group input[type='text'], +.form-group input[type='email'], +.form-group input[type='number'], +.form-group input[type='password'] { display: block; width: 100%; height: 34px; @@ -52,7 +55,7 @@ form { .validation-error { color: red; - margin-top: 5px; + margin: 5px 0; display: inline-block; } diff --git a/examples/index.html b/examples/index.html index fd76d10..d813a18 100644 --- a/examples/index.html +++ b/examples/index.html @@ -8,6 +8,7 @@

Formsy React Examples

\ No newline at end of file