From 0d34e7b5bb5edda479e7100edcef6628094ee4df Mon Sep 17 00:00:00 2001 From: Philipp Otto Date: Mon, 23 Mar 2015 00:16:05 +0100 Subject: [PATCH] Allow a component to decide whether it is valid (fixes #60) --- specs/Validation-spec.js | 31 +++++++++++++++++++++++++++++++ src/main.js | 7 ++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/specs/Validation-spec.js b/specs/Validation-spec.js index 2876286..fd2a79c 100644 --- a/specs/Validation-spec.js +++ b/specs/Validation-spec.js @@ -50,6 +50,37 @@ describe('Validation', function() { }); + it('should use provided checkValidity function', function () { + + var isValid = jasmine.createSpy('valid'); + var TestInput = React.createClass({ + mixins: [Formsy.Mixin], + updateValue: function (event) { + this.setValue(event.target.value); + }, + render: function () { + if (this.isValid()) { + isValid(); + } + return + }, + checkValidity: function () { + return this.getValue() === "checkValidity"; + } + }); + var form = TestUtils.renderIntoDocument( + + + + ); + + var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); + expect(isValid).not.toHaveBeenCalled(); + TestUtils.Simulate.change(input, {target: {value: 'checkValidity'}}); + expect(isValid).toHaveBeenCalled(); + + }); + it('RULE: isEmail', function () { var isValid = jasmine.createSpy('valid'); diff --git a/src/main.js b/src/main.js index aec26b2..8d8dcc6 100644 --- a/src/main.js +++ b/src/main.js @@ -212,7 +212,7 @@ Formsy.Form = React.createClass({ this.props.onChange(this.getCurrentValues()); } - if (!component.props.required && !component._validations) { + if (!component.props.required && !component._validations && !component.checkValidity) { return; } @@ -249,6 +249,11 @@ Formsy.Form = React.createClass({ } }.bind(this)); } + if (typeof component.checkValidity === "function") { + // the component defines an explicit checkValidity function + isValid = component.checkValidity() + } + return isValid; },