Merge pull request #63 from philippotto/checkValidity

Allow a component to decide whether it is valid (fixes #60)
This commit is contained in:
Christian Alfoni 2015-04-01 13:43:18 +02:00
commit eb3eaa6544
2 changed files with 37 additions and 1 deletions

View File

@ -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 <input value={this.getValue()} onChange={this.updateValue}/>
},
checkValidity: function () {
return this.getValue() === "checkValidity";
}
});
var form = TestUtils.renderIntoDocument(
<Formsy.Form>
<TestInput name="foo" value="checkInvalidity"/>
</Formsy.Form>
);
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');

View File

@ -232,7 +232,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;
}
@ -273,6 +273,11 @@ Formsy.Form = React.createClass({
}
}.bind(this));
}
if (typeof component.checkValidity === "function") {
// the component defines an explicit checkValidity function
isValid = component.checkValidity()
}
return isValid;
},