Skip to content

Commit

Permalink
Changed revalidation rules (closes #82).
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie committed Jul 22, 2016
1 parent 81ba23f commit 6fbf046
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
16 changes: 10 additions & 6 deletions packages/uniforms/src/components/forms/ValidatedForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,26 @@ const Validated = parent => class extends parent {
return props;
}

componentWillReceiveProps ({model, schema, validator}) {
componentWillReceiveProps ({model, schema, validate, validator}) {
super.componentWillReceiveProps(...arguments);

if (this.props.schema !== schema ||
this.props.validator !== validator) {
this.setState({
validate: true,
validator: this
.getChildContextSchema()
.getValidator(validator)
}, () => {
if (validate === 'onChange' ||
validate === 'onChangeAfterSubmit' && this.state.validate) {
this.onValidate();
}
});

this.onValidate();
} else if (!isEqual(this.props.model, model)) {
this.setState({validate: true});
this.onValidate();
if (validate === 'onChange' ||
validate === 'onChangeAfterSubmit' && this.state.validate) {
this.onValidate();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/uniforms/test/components/forms/AutoForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('AutoForm', () => {
});

it('validates', () => {
wrapper.setProps({model});
wrapper.setProps({model, validate: 'onChange'});

expect(validator.calledOnce).to.be.ok;
});
Expand Down
34 changes: 32 additions & 2 deletions packages/uniforms/test/components/forms/ValidatedForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('ValidatedForm', () => {
expect(onSubmit.called).to.be.false;
});

it('revalidates with new model', () => {
it('revalidates with new model only if required', () => {
validator.onFirstCall().throws();
validator.onSecondCall().returns();

Expand All @@ -101,6 +101,21 @@ describe('ValidatedForm', () => {

wrapper.setProps({model});

expect(validator.called).to.be.false;
});

it('revalidates with new model', () => {
validator.onFirstCall().throws();
validator.onSecondCall().returns();

const wrapper = mount(
<ValidatedForm model={{}} schema={schema} validate="onChange" />
);

expect(validator.called).to.be.false;

wrapper.setProps({model});

expect(validator.calledOnce).to.be.ok;
});

Expand All @@ -119,6 +134,21 @@ describe('ValidatedForm', () => {
expect(validator.called).to.be.false;
});

it('revalidates with new validator only if required', () => {
validator.onFirstCall().throws();
validator.onSecondCall().returns();

const wrapper = mount(
<ValidatedForm model={{}} schema={schema} validate="onChange" />
);

expect(validator.called).to.be.false;

wrapper.setProps({model, validator: {}});

expect(validator.calledOnce).to.be.ok;
});

it('revalidates with new validator', () => {
validator.onFirstCall().throws();
validator.onSecondCall().returns();
Expand All @@ -131,7 +161,7 @@ describe('ValidatedForm', () => {

wrapper.setProps({model, validator: {}});

expect(validator.calledOnce).to.be.ok;
expect(validator.called).to.be.false;
});

it('validates (onChange)', () => {
Expand Down

0 comments on commit 6fbf046

Please sign in to comment.