From 50af2f389b27a041014a48db08b8c784029d80e5 Mon Sep 17 00:00:00 2001 From: Duy Lam Date: Thu, 3 Dec 2015 22:24:52 +0700 Subject: [PATCH 1/2] Add validateOnMount option on Form to skip validating at first render + update API.md --- API.md | 7 +++++++ src/main.js | 10 +++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/API.md b/API.md index fa1a1983..114e961d 100644 --- a/API.md +++ b/API.md @@ -13,6 +13,7 @@ - [onChange()](#onchange) - [reset()](#resetform) - [preventExternalInvalidation](#preventexternalinvalidation) + - [validateOnMount](#validateOnMount) - [Formsy.Mixin](#formsymixin) - [name](#name) - [value](#value) @@ -181,6 +182,12 @@ var MyForm = React.createClass({ ``` With the `preventExternalInvalidation` the input will not be invalidated though it has an error. +#### validateOnMount +```html + +``` +`validateOnMount` indicates that whether Formsy.Form validates all its inputs immediately after the initial rendering occurs (e.g componentDidMount). By default it's `true` e.g validate on componentDidMount + ### Formsy.Mixin #### name diff --git a/src/main.js b/src/main.js index 60b7bef7..7650da10 100644 --- a/src/main.js +++ b/src/main.js @@ -42,6 +42,7 @@ Formsy.Form = React.createClass({ onInvalid: function () {}, onChange: function () {}, validationErrors: null, + validateOnMount: true, // avoid break change preventExternalInvalidation: false }; }, @@ -49,6 +50,7 @@ Formsy.Form = React.createClass({ childContextTypes: { formsy: React.PropTypes.object }, + getChildContext: function () { return { formsy: { @@ -71,19 +73,18 @@ Formsy.Form = React.createClass({ }, componentDidMount: function () { - this.validateForm(); + if (this.props.validateOnMount) { + this.validateForm(); + } }, componentWillUpdate: function () { - // Keep a reference to input keys before form updates, // to check if inputs has changed after render this.prevInputKeys = Object.keys(this.inputs); - }, componentDidUpdate: function () { - if (this.props.validationErrors && typeof this.props.validationErrors === 'object' && Object.keys(this.props.validationErrors).length > 0) { this.setInputValidationErrors(this.props.validationErrors); } @@ -92,7 +93,6 @@ Formsy.Form = React.createClass({ if (utils.arraysDiffer(this.prevInputKeys, newInputKeys)) { this.validateForm(); } - }, // Allow resetting to specified data From b4d03e2549c5aaa1b1e93c5af180b877719bddad Mon Sep 17 00:00:00 2001 From: Duy Lam Date: Mon, 28 Dec 2015 22:36:55 +0700 Subject: [PATCH 2/2] Add missing case for conditional validating form on "mount" event --- src/main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index 92055a98..9f3d2e04 100644 --- a/src/main.js +++ b/src/main.js @@ -411,7 +411,9 @@ Formsy.Form = React.createClass({ this.inputs.push(component); } - this.validate(component); + if (this.props.validateOnMount) { + this.validate(component); + } }, // Method put on each input component to unregister