From a72838b4462fbb1adf04f3cc95ac9a4f8235b6e4 Mon Sep 17 00:00:00 2001 From: Bryan Naegele Date: Mon, 23 Feb 2015 14:33:06 -0600 Subject: [PATCH 1/3] Check if form is mounted before calling setState Check if form is mounted before set state. Cleaned up calls to prop functions. --- src/main.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.js b/src/main.js index 0bd844e..98f0eae 100644 --- a/src/main.js +++ b/src/main.js @@ -404,7 +404,9 @@ Formsy.Form = React.createClass({ validate: function (component) { // Trigger onChange - this.state.canChange && this.props.onChange && this.props.onChange(this.getCurrentValues()); + if(this.state.canChange) { + this.props.onChange(this.getCurrentValues()); + } if (!component.props.required && !component._validations) { return; @@ -466,8 +468,11 @@ Formsy.Form = React.createClass({ isValid: allIsValid }); - allIsValid && this.props.onValid(); - !allIsValid && this.props.onInvalid(); + if(allIsValid) { + this.props.onValid(); + } else { + this.props.onInvalid(); + } // Tell the form that it can start to trigger change events this.setState({canChange: true}); @@ -487,9 +492,11 @@ Formsy.Form = React.createClass({ // If there are no inputs, it is ready to trigger change events if (!inputKeys.length) { - this.setState({canChange: true}); + // but make sure the component is mounted + if(this.isMounted()){ + this.setState({canChange: true}); + } } - }, // Method put on each input component to register From 5cc979e2490ee0ae67465c6da25a1410a3ec0976 Mon Sep 17 00:00:00 2001 From: Bryan Naegele Date: Mon, 2 Mar 2015 13:08:08 -0600 Subject: [PATCH 2/3] Dynamic validation support Add dynamic validation support --- src/main.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main.js b/src/main.js index 98f0eae..f0a8b91 100644 --- a/src/main.js +++ b/src/main.js @@ -127,13 +127,7 @@ Formsy.Mixin = { componentWillMount: function () { var configure = function () { - // Add validations to the store itself as the props object can not be modified - this._validations = this.props.validations || ''; - - if (this.props.required) { - this._validations = this.props.validations ? this.props.validations + ',' : ''; - this._validations += 'isValue'; - } + this.setValidations(); this.props._attachToForm(this); }.bind(this); @@ -158,6 +152,7 @@ Formsy.Mixin = { nextProps._attachToForm = this.props._attachToForm; nextProps._detachFromForm = this.props._detachFromForm; nextProps._validate = this.props._validate; + this.setValidations(); }, componentDidUpdate: function(prevProps, prevState) { @@ -177,6 +172,16 @@ Formsy.Mixin = { this.props._detachFromForm(this); }, + setValidations: function() { + // Add validations to the store itself as the props object can not be modified + this._validations = this.props.validations || ''; + + if (this.props.required) { + this._validations = this.props.validations ? this.props.validations + ',' : ''; + this._validations += 'isValue'; + } + }, + // We validate after the value has been set setValue: function (value) { this.setState({ From 7fb17a752de0b35b43795e93c2129401599b38cf Mon Sep 17 00:00:00 2001 From: Bryan Naegele Date: Mon, 2 Mar 2015 13:22:11 -0600 Subject: [PATCH 3/3] Dynamic validations and requiring Allow validations to be updated, including requiring --- src/main.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.js b/src/main.js index f0a8b91..c6d7fff 100644 --- a/src/main.js +++ b/src/main.js @@ -127,7 +127,7 @@ Formsy.Mixin = { componentWillMount: function () { var configure = function () { - this.setValidations(); + this.setValidations(this.props.validations, this.props.required); this.props._attachToForm(this); }.bind(this); @@ -152,7 +152,7 @@ Formsy.Mixin = { nextProps._attachToForm = this.props._attachToForm; nextProps._detachFromForm = this.props._detachFromForm; nextProps._validate = this.props._validate; - this.setValidations(); + this.setValidations(nextProps.validations, nextProps.required); }, componentDidUpdate: function(prevProps, prevState) { @@ -172,12 +172,12 @@ Formsy.Mixin = { this.props._detachFromForm(this); }, - setValidations: function() { + setValidations: function(validations, required) { // Add validations to the store itself as the props object can not be modified - this._validations = this.props.validations || ''; + this._validations = validations || ''; - if (this.props.required) { - this._validations = this.props.validations ? this.props.validations + ',' : ''; + if (required) { + this._validations = validations ? validations + ',' : ''; this._validations += 'isValue'; } },