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

Commit c7f51a3

Browse files
committed
Removing unused (and hacky) callback functions in actions.setFieldsValidity()
1 parent a86276c commit c7f51a3

File tree

2 files changed

+0
-163
lines changed

2 files changed

+0
-163
lines changed

src/actions/field-actions.js

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import invertValidity from '../utils/invert-validity';
1010
import { trackable } from '../utils/track';
1111
import getForm from '../utils/get-form';
1212
import getFieldFromState from '../utils/get-field-from-state';
13-
import isValid from '../form/is-valid';
1413
import NULL_ACTION from '../constants/null-action';
1514
import omit from 'lodash/omit';
1615
import isNative from '../utils/is-native';
@@ -236,27 +235,6 @@ function createFieldActions(s = defaultStrategies) {
236235
dispatch(setValidity(model, errors, { errors: true }));
237236
};
238237

239-
function isFormValidWithoutFields(form, fieldsValidity) {
240-
if (Object.keys(form.$form.validity).length
241-
&& !isValidityValid(form.$form.validity)) {
242-
return false;
243-
}
244-
245-
// TODO: map through form keys without $form
246-
const valid = Object.keys(form)
247-
.every((fieldKey) => {
248-
if (fieldKey === '$form') return true;
249-
250-
if (fieldsValidity.hasOwnProperty(fieldKey)) {
251-
return true;
252-
}
253-
254-
return isValid(form[fieldKey]);
255-
});
256-
257-
return valid;
258-
}
259-
260238
const validateFields =
261239
(model, fieldValidators, options = {}) => (dispatch, getState) => {
262240
const modelValue = s.get(getState(), model);
@@ -271,26 +249,6 @@ function createFieldActions(s = defaultStrategies) {
271249
return fieldValidity;
272250
});
273251

274-
const validCB = options.onValid;
275-
const invalidCB = options.onInvalid;
276-
277-
if (validCB || invalidCB) {
278-
const form = s.getForm(getState(), model);
279-
const formValid = (form && !fieldsValidity.hasOwnProperty(''))
280-
? isFormValidWithoutFields(form, fieldsValidity)
281-
: true;
282-
283-
const fieldsValid = options.errors
284-
? !isValidityInvalid(fieldsValidity)
285-
: isValidityValid(fieldsValidity);
286-
287-
if (validCB && formValid && fieldsValid) {
288-
validCB();
289-
} else if (invalidCB) {
290-
invalidCB();
291-
}
292-
}
293-
294252
const fieldsValiditySetter = options.errors
295253
? setFieldsErrors
296254
: setFieldsValidity;

test/field-actions-spec.js

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { assert } from 'chai';
22
import configureMockStore from 'redux-mock-store';
33
import createTestStore from 'redux-test-store';
44
import thunk from 'redux-thunk';
5-
import sinon from 'sinon';
65
import _get from 'lodash/get';
76
import toPath from 'lodash/toPath';
87
import i from 'icepick';
@@ -1602,64 +1601,6 @@ Object.keys(testContexts).forEach((testKey) => {
16021601

16031602
store.dispatch(action);
16041603
});
1605-
1606-
it('should call a callback if validation passes', (done) => {
1607-
const callback = sinon.spy((val) => val);
1608-
1609-
const validationOptions = {
1610-
onValid: callback,
1611-
};
1612-
1613-
const store = mockStore(
1614-
() => ({ test: { foo: 'bar' } }),
1615-
[{
1616-
model: 'test',
1617-
type: actionTypes.SET_FIELDS_VALIDITY,
1618-
fieldsValidity: {
1619-
foo: true,
1620-
},
1621-
options: {},
1622-
}],
1623-
() => {
1624-
assert.isTrue(callback.calledOnce);
1625-
done();
1626-
});
1627-
1628-
const action = actions.validateFields('test', {
1629-
foo: (val) => val === 'bar',
1630-
}, validationOptions);
1631-
1632-
store.dispatch(action);
1633-
});
1634-
1635-
it('should NOT call a callback if validation fails', (done) => {
1636-
const callback = sinon.spy((val) => val);
1637-
1638-
const validationOptions = {
1639-
onValid: callback,
1640-
};
1641-
1642-
const store = mockStore(
1643-
() => ({ test: { foo: 'bar' } }),
1644-
[{
1645-
model: 'test',
1646-
type: actionTypes.SET_FIELDS_VALIDITY,
1647-
fieldsValidity: {
1648-
foo: false,
1649-
},
1650-
options: {},
1651-
}],
1652-
() => {
1653-
assert.isTrue(callback.notCalled);
1654-
done();
1655-
});
1656-
1657-
const action = actions.validateFields('test', {
1658-
foo: (val) => val === 'invalid',
1659-
}, validationOptions);
1660-
1661-
store.dispatch(action);
1662-
});
16631604
});
16641605

16651606
describe('validateFieldsErrors() (thunk)', () => {
@@ -1702,68 +1643,6 @@ Object.keys(testContexts).forEach((testKey) => {
17021643

17031644
store.dispatch(action);
17041645
});
1705-
1706-
it('should call a callback if validation passes', (done) => {
1707-
const callback = sinon.spy((val) => val);
1708-
1709-
const validationOptions = {
1710-
onValid: callback,
1711-
};
1712-
1713-
const store = mockStore(
1714-
() => ({ test: { foo: 'valid' } }),
1715-
[{
1716-
model: 'test',
1717-
type: actionTypes.SET_FIELDS_VALIDITY,
1718-
fieldsValidity: {
1719-
foo: false, // false = not an error
1720-
},
1721-
options: {
1722-
errors: true,
1723-
},
1724-
}],
1725-
() => {
1726-
assert.isTrue(callback.calledOnce);
1727-
done();
1728-
});
1729-
1730-
const action = actions.validateFieldsErrors('test', {
1731-
foo: (val) => val === 'invalid',
1732-
}, validationOptions);
1733-
1734-
store.dispatch(action);
1735-
});
1736-
1737-
it('should NOT call a callback if validation fails', (done) => {
1738-
const callback = sinon.spy((val) => val);
1739-
1740-
const validationOptions = {
1741-
onValid: callback,
1742-
};
1743-
1744-
const store = mockStore(
1745-
() => ({ test: { foo: 'invalid' } }),
1746-
[{
1747-
model: 'test',
1748-
type: actionTypes.SET_FIELDS_VALIDITY,
1749-
fieldsValidity: {
1750-
foo: true, // true = error
1751-
},
1752-
options: {
1753-
errors: true,
1754-
},
1755-
}],
1756-
() => {
1757-
assert.isTrue(callback.notCalled);
1758-
done();
1759-
});
1760-
1761-
const action = actions.validateFieldsErrors('test', {
1762-
foo: (val) => val === 'invalid',
1763-
}, validationOptions);
1764-
1765-
store.dispatch(action);
1766-
});
17671646
});
17681647

17691648
describe('validSubmit() (thunk)', () => {

0 commit comments

Comments
 (0)