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

Commit 652812e

Browse files
committed
Fixing validity reset issue where getFieldFromState always returned a field (now retursn null if field does not exist). Fixes #425
1 parent 2d5d308 commit 652812e

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

docs/api/actions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,13 +804,16 @@ dispatch(actions.validateFieldsErrors('user', {
804804
```
805805
806806
<h2 id="actions-resetValidity"></h2>
807-
## `actions.resetValidity(model)`
807+
## `actions.resetValidity(model, [omitKeys])`
808808
Resets the `.validity` and `.errors` for the field model to the `.validity` and `.errors` of the initial field state.
809809
810+
If `omitKeys` is specified as an array, it will only reset the validity to the omitted keys by removing them from the current validity/errors.
811+
810812
_Alias:_ `actions.resetErrors(model)`
811813
812814
### Arguments
813815
- `model` _(String | Function)_: the model whose validity and errors will be reset.
816+
- `omitKeys` _(Array)_: the keys to reset by omitting them. Default: `false` (will reset all keys)
814817
815818
816819

src/actions/field-actions.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import isValidityInvalid from '../utils/is-validity-invalid';
99
import invertValidity from '../utils/invert-validity';
1010
import { trackable } from '../utils/track';
1111
import getForm from '../utils/get-form';
12+
import getFieldFromState from '../utils/get-field-from-state';
1213
import isValid from '../form/is-valid';
1314
import NULL_ACTION from '../constants/null-action';
15+
import omit from 'lodash/omit';
1416

1517
const focus = trackable((model) => ({
1618
type: actionTypes.FOCUS,
@@ -57,6 +59,29 @@ const setValidity = trackable((model, validity, options = {}) => ({
5759
[options.errors ? 'errors' : 'validity']: validity,
5860
}));
5961

62+
const resetValidity = trackable((model, omitKeys = false) => {
63+
if (!omitKeys) {
64+
return {
65+
type: actionTypes.RESET_VALIDITY,
66+
model,
67+
};
68+
}
69+
70+
return (dispatch, getState) => {
71+
const field = getFieldFromState(getState(), model);
72+
73+
if (!field) {
74+
dispatch(NULL_ACTION);
75+
} else {
76+
dispatch({
77+
type: actionTypes.SET_VALIDITY,
78+
model,
79+
validity: omit(field.validity, omitKeys),
80+
});
81+
}
82+
};
83+
});
84+
6085
const setFieldsValidity = trackable((model, fieldsValidity, options = {}) => ({
6186
type: actionTypes.SET_FIELDS_VALIDITY,
6287
model,
@@ -76,11 +101,6 @@ const setFieldsErrors = trackable((model, fieldsErrors, options) =>
76101
errors: true,
77102
}));
78103

79-
const resetValidity = trackable((model) => ({
80-
type: actionTypes.RESET_VALIDITY,
81-
model,
82-
}));
83-
84104
const resetErrors = resetValidity;
85105

86106
const setTouched = trackable((model) => ({

src/components/control-component.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import validityKeys from '../constants/validity-keys';
2323
import { dispatchBatchIfNeeded } from '../actions/batch-actions';
2424
import resolveModel from '../utils/resolve-model';
2525
import isNative from '../utils/is-native';
26+
import { initialFieldState } from '../reducers/form-reducer';
2627

2728
const findDOMNode = !isNative
2829
? require('react-dom').findDOMNode
@@ -96,7 +97,8 @@ function mapStateToProps(state, props) {
9697
} = props;
9798

9899
const modelString = getModel(model, state);
99-
const fieldValue = getFieldFromState(state, modelString);
100+
const fieldValue = getFieldFromState(state, modelString)
101+
|| initialFieldState;
100102

101103
return {
102104
model: modelString,
@@ -192,7 +194,7 @@ class Control extends Component {
192194
const keys = Object.keys(validators)
193195
.concat(Object.keys(errors));
194196

195-
dispatch(actions.setValidity(model, omit(fieldValue.validity, keys)));
197+
dispatch(actions.resetValidity(model, keys));
196198
}
197199
}
198200

src/components/errors-component.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import getFieldFromState from '../utils/get-field-from-state';
1313
import getModel from '../utils/get-model';
1414
import isValid from '../form/is-valid';
1515
import resolveModel from '../utils/resolve-model';
16+
import { initialFieldState } from '../reducers/form-reducer';
1617

1718
function showErrors(field, form, show = true) {
1819
if (typeof show === 'function') {
@@ -163,7 +164,8 @@ function mapStateToProps(state, { model }) {
163164
const modelString = getModel(model, state);
164165

165166
const formValue = getForm(state, modelString).$form;
166-
const fieldValue = getFieldFromState(state, modelString);
167+
const fieldValue = getFieldFromState(state, modelString)
168+
|| initialFieldState;
167169

168170
return {
169171
model: modelString,

src/components/field-component.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import shallowCompareWithoutChildren from '../utils/shallow-compare-without-chil
1515
import getModel from '../utils/get-model';
1616
import getFieldFromState from '../utils/get-field-from-state';
1717
import resolveModel from '../utils/resolve-model';
18+
import { initialFieldState } from '../reducers/form-reducer';
1819

1920
const fieldPropTypes = {
2021
model: PropTypes.oneOfType([
@@ -67,7 +68,8 @@ function mapStateToProps(state, props) {
6768
} = props;
6869

6970
const modelString = getModel(model, state);
70-
const fieldValue = getFieldFromState(state, modelString);
71+
const fieldValue = getFieldFromState(state, modelString)
72+
|| initialFieldState;
7173

7274
return {
7375
model: modelString,

src/reducers/form/change-action-reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function updateFieldValue(field, action) {
3737
result = [];
3838

3939
Object.keys(field).forEach((key) => {
40-
if (!!~removeKeysArray.indexOf(+key) || key === '$form') return;
40+
if (!!~removeKeysArray.indexOf(+key) || (key === '$form')) return;
4141

4242
result[key] = field[key];
4343
});

src/utils/get-field-from-state.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import get from './get';
22
import toPath from './to-path';
3-
import { initialFieldState } from '../reducers/form-reducer';
43
import getForm from './get-form';
54
import isPlainObject from 'lodash/isPlainObject';
65

@@ -13,11 +12,10 @@ export default function getFieldFromState(state, modelString) {
1312

1413
const formPath = toPath(form.$form.model);
1514
const fieldPath = toPath(modelString).slice(formPath.length);
16-
const field = get(form, fieldPath, initialFieldState);
15+
const field = get(form, fieldPath);
1716

17+
if (!field) return null;
1818
if (isPlainObject(field) && '$form' in field) return field.$form;
1919

20-
if (!field) return initialFieldState;
21-
2220
return field;
2321
}

0 commit comments

Comments
 (0)