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

Commit f2d341f

Browse files
committed
Adding invalid callback for validateFields action, fixes #89 + unit tests
1 parent e82f5cb commit f2d341f

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/actions/field-actions.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const validateErrors = (model, errorValidators) => (dispatch, getState) => {
131131
dispatch(setErrors(model, errors));
132132
};
133133

134-
const validateFields = (model, fieldValidators, callback) => (dispatch, getState) => {
134+
const validateFields = (model, fieldValidators, validCB, invalidCB) => (dispatch, getState) => {
135135
const value = _get(getState(), model);
136136

137137
const fieldsValidity = mapValues(fieldValidators, (validator, field) => {
@@ -144,12 +144,14 @@ const validateFields = (model, fieldValidators, callback) => (dispatch, getState
144144
return fieldValidity;
145145
});
146146

147-
if (callback) {
147+
if (validCB || invalidCB) {
148148
const form = getForm(getState(), model);
149149
const formValid = form ? form.valid : true;
150150

151-
if (formValid && isValid(fieldsValidity)) {
152-
callback();
151+
if (validCB && formValid && isValid(fieldsValidity)) {
152+
validCB();
153+
} else if (invalidCB) {
154+
invalidCB();
153155
}
154156
}
155157

src/components/form-component.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import mapValues from 'lodash/mapValues';
88
import actions from '../actions';
99
import { getValidity, getForm } from '../utils';
1010

11+
function dispatchValidCallback(modelValue, callback) {
12+
return callback
13+
? () => callback(modelValue)
14+
: undefined;
15+
}
16+
17+
function dispatchInvalidCallback(model, dispatch) {
18+
return () => dispatch(actions.setSubmitFailed(model));
19+
}
20+
1121
class Form extends Component {
1222
constructor(props) {
1323
super(props);
@@ -86,14 +96,14 @@ class Form extends Component {
8696
return modelValue;
8797
}
8898

89-
const validationCallback = onSubmit
90-
? () => onSubmit(modelValue)
91-
: undefined;
99+
const validCallback = dispatchValidCallback(modelValue, onSubmit);
100+
const invalidCallback = dispatchInvalidCallback(model, dispatch);
92101

93102
dispatch(actions.validateFields(
94103
model,
95104
validators,
96-
validationCallback));
105+
validCallback,
106+
invalidCallback));
97107

98108
return modelValue;
99109
}

test/form-component-spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ describe('<Form> component', () => {
427427
assert.isNull(submitValue);
428428
});
429429

430+
it('should set submitFailed to true if form is invalid and submitted', () => {
431+
TestUtils.Simulate.submit(formElement);
432+
433+
assert.isTrue(store.getState().testForm.submitFailed);
434+
});
435+
430436
it('should call onSubmit with model value if form is valid', () => {
431437
barControl.value = 'bar';
432438

0 commit comments

Comments
 (0)