Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 6c641dd

Browse files
committed
Fixing issue where async form-level validity should be invalidated (set to true) after submit failed and form changed.
1 parent d24628c commit 6c641dd

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/components/form-component.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ class Form extends Component {
4949
model,
5050
dispatch,
5151
formValue,
52+
modelValue,
5253
} = this.props;
5354

55+
if (!validators && !errors && (modelValue !== nextProps.modelValue)) {
56+
dispatch(actions.setValidity(model, true));
57+
return;
58+
}
59+
5460
let validityChanged = false;
5561

5662
const fieldsValidity = mapValues(validators, (validator, field) => {

test/form-component-spec.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import TestUtils from 'react-addons-test-utils';
55
import { applyMiddleware, combineReducers, createStore } from 'redux';
66
import { Provider } from 'react-redux';
77
import thunk from 'redux-thunk';
8+
import createTestStore from 'redux-test-store';
89

9-
import { Form, modelReducer, formReducer, Field, actions } from '../src';
10+
import { Form, modelReducer, formReducer, Field, actions, actionTypes } from '../src';
1011

1112
describe('<Form> component', () => {
1213
describe('wraps component if specified', () => {
@@ -930,15 +931,15 @@ describe('<Form> component', () => {
930931
});
931932

932933
describe('deep state path', () => {
933-
const fromsReducer = combineReducers({
934+
const formsReducer = combineReducers({
934935
testForm: formReducer('forms.test'),
935936
test: modelReducer('forms.test', {
936937
foo: '',
937938
bar: '',
938939
}),
939940
});
940941
const store = applyMiddleware(thunk)(createStore)(combineReducers({
941-
forms: fromsReducer,
942+
forms: formsReducer,
942943
}));
943944

944945
const form = TestUtils.renderIntoDocument(
@@ -958,4 +959,49 @@ describe('<Form> component', () => {
958959
assert.containSubset(props.formValue, { valid: true, model: 'forms.test' });
959960
});
960961
});
962+
963+
describe('invalidating async validity on form change', () => {
964+
const store = createTestStore(applyMiddleware(thunk)(createStore)(combineReducers({
965+
test: modelReducer('test', { val: 'invalid' }),
966+
testForm: formReducer('test', { val: 'invalid' }),
967+
})));
968+
969+
function handleSubmit() {
970+
const promise = new Promise((resolve, reject) => reject('Form is invalid'));
971+
972+
store.dispatch(actions.submit('test', promise));
973+
}
974+
975+
const form = TestUtils.renderIntoDocument(
976+
<Provider store={store}>
977+
<Form model="test"
978+
onSubmit={handleSubmit}
979+
>
980+
<Field model="test.foo">
981+
<input type="text" />
982+
</Field>
983+
</Form>
984+
</Provider>
985+
);
986+
987+
const formElement = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
988+
const inputElement = TestUtils.findRenderedDOMComponentWithTag(form, 'input');
989+
990+
it('should set errors from rejected submit handler on valid submit', (done) => {
991+
store.when(actionTypes.SET_ERRORS, (state) => {
992+
assert.isFalse(state.testForm.valid);
993+
assert.equal(state.testForm.errors, 'Form is invalid');
994+
done();
995+
});
996+
997+
TestUtils.Simulate.submit(formElement);
998+
});
999+
1000+
it('should set validity on form changes after submit failed', () => {
1001+
inputElement.value = 'valid';
1002+
TestUtils.Simulate.change(inputElement);
1003+
1004+
assert.isTrue(store.getState().testForm.valid);
1005+
});
1006+
});
9611007
});

0 commit comments

Comments
 (0)