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

Commit 54ca747

Browse files
committed
Adding setFieldsErrors action thunk creator and reducing argument length for validateFields()
1 parent cae1f1f commit 54ca747

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

src/actions/field-actions.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,24 @@ const setValidity = (model, validity, options = {}) => ({
4343
[options.errors ? 'errors' : 'validity']: validity,
4444
});
4545

46-
const setFieldsValidity = (model, fieldsValidity) => ({
46+
const setFieldsValidity = (model, fieldsValidity, options = {}) => ({
4747
type: actionTypes.SET_FIELDS_VALIDITY,
4848
model,
4949
fieldsValidity,
50+
options,
5051
});
5152

52-
// will be deprecated
53-
const setErrors = (model, errors, options = {}) => setValidity(model, errors, {
54-
errors: true,
55-
...options,
56-
});
53+
const setErrors = (model, errors, options = {}) =>
54+
setValidity(model, errors, {
55+
...options,
56+
errors: true,
57+
});
58+
59+
const setFieldsErrors = (model, fieldsErrors, options) =>
60+
setFieldsValidity(model, fieldsErrors, {
61+
...options,
62+
errors: true,
63+
});
5764

5865
const resetValidity = (model) => ({
5966
type: actionTypes.RESET_VALIDITY,
@@ -133,7 +140,7 @@ const validateErrors = (model, errorValidators) => (dispatch, getState) => {
133140
dispatch(setValidity(model, errors, { errors: true }));
134141
};
135142

136-
const validateFields = (model, fieldValidators, validCB, invalidCB) => (dispatch, getState) => {
143+
const validateFields = (model, fieldValidators, options = {}) => (dispatch, getState) => {
137144
const value = _get(getState(), model);
138145

139146
const fieldsValidity = mapValues(fieldValidators, (validator, field) => {
@@ -146,6 +153,9 @@ const validateFields = (model, fieldValidators, validCB, invalidCB) => (dispatch
146153
return fieldValidity;
147154
});
148155

156+
const validCB = options.onValid;
157+
const invalidCB = options.onInvalid;
158+
149159
if (validCB || invalidCB) {
150160
const form = getForm(getState(), model);
151161
const formValid = form ? form.valid : true;
@@ -175,6 +185,8 @@ export default {
175185
setTouched,
176186
setUntouched,
177187
setValidity,
188+
setFieldsValidity,
189+
setFieldsErrors,
178190
resetValidity,
179191
resetErrors,
180192
setViewValue,

src/components/form-component.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ class Form extends Component {
9696
return modelValue;
9797
}
9898

99-
const validCallback = dispatchValidCallback(modelValue, onSubmit);
100-
const invalidCallback = dispatchInvalidCallback(model, dispatch);
99+
const validationOptions = {
100+
onValid: dispatchValidCallback(modelValue, onSubmit),
101+
onInvalid: dispatchInvalidCallback(model, dispatch),
102+
};
101103

102104
dispatch(actions.validateFields(
103105
model,
104106
validators,
105-
validCallback,
106-
invalidCallback));
107+
validationOptions));
107108

108109
return modelValue;
109110
}

src/reducers/form-reducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function _createFormReducer(model, initialState) {
210210
type: actionTypes.SET_VALIDITY,
211211
model: `${model}.${field}`,
212212
validity: fieldValidity,
213+
options: action.options,
213214
})).reduce(formReducer, state);
214215

215216
case actionTypes.SET_ERRORS: {

test/field-actions-spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,7 @@ describe('field actions', () => {
13031303
},
13041304
model: 'test',
13051305
type: actionTypes.SET_FIELDS_VALIDITY,
1306+
options: {},
13061307
},
13071308
],
13081309
done);
@@ -1324,6 +1325,10 @@ describe('field actions', () => {
13241325
it('should call a callback if validation passes', (done) => {
13251326
const callback = sinon.spy((val) => val);
13261327

1328+
const validationOptions = {
1329+
onValid: callback,
1330+
};
1331+
13271332
const store = mockStore(
13281333
() => ({ test: { foo: 'bar' } }),
13291334
[{
@@ -1332,6 +1337,7 @@ describe('field actions', () => {
13321337
fieldsValidity: {
13331338
foo: true,
13341339
},
1340+
options: {},
13351341
}],
13361342
() => {
13371343
assert.isTrue(callback.calledOnce);
@@ -1340,14 +1346,18 @@ describe('field actions', () => {
13401346

13411347
const action = actions.validateFields('test', {
13421348
foo: (val) => val === 'bar',
1343-
}, callback);
1349+
}, validationOptions);
13441350

13451351
store.dispatch(action);
13461352
});
13471353

13481354
it('should NOT call a callback if validation fails', (done) => {
13491355
const callback = sinon.spy((val) => val);
13501356

1357+
const validationOptions = {
1358+
onValid: callback,
1359+
};
1360+
13511361
const store = mockStore(
13521362
() => ({ test: { foo: 'bar' } }),
13531363
[{
@@ -1356,6 +1366,7 @@ describe('field actions', () => {
13561366
fieldsValidity: {
13571367
foo: false,
13581368
},
1369+
options: {},
13591370
}],
13601371
() => {
13611372
assert.isTrue(callback.notCalled);
@@ -1364,7 +1375,7 @@ describe('field actions', () => {
13641375

13651376
const action = actions.validateFields('test', {
13661377
foo: (val) => val === 'invalid',
1367-
}, callback);
1378+
}, validationOptions);
13681379

13691380
store.dispatch(action);
13701381
});

0 commit comments

Comments
 (0)