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

Commit b008e0e

Browse files
committed
Ensuring that unmounting a component is subtractive, not additive, w/r/t validity (and HTML5 validation attributes). Fixes #580
1 parent fdc84ba commit b008e0e

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

src/actions/field-actions.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { trackable } from '../utils/track';
1111
import getForm from '../utils/get-form';
1212
import getFieldFromState from '../utils/get-field-from-state';
1313
import NULL_ACTION from '../constants/null-action';
14-
import omit from '../utils/omit';
1514
import isNative from '../utils/is-native';
1615
import invariant from 'invariant';
1716

@@ -86,28 +85,11 @@ function createFieldActions(s = defaultStrategies) {
8685
[options.errors ? 'errors' : 'validity']: validity,
8786
});
8887

89-
const resetValidity = (model, omitKeys = false) => {
90-
if (!omitKeys) {
91-
return {
92-
type: actionTypes.RESET_VALIDITY,
93-
model,
94-
};
95-
}
96-
97-
return (dispatch, getState) => {
98-
const field = s.getFieldFromState(getState(), model);
99-
100-
if (!field) {
101-
dispatch(NULL_ACTION);
102-
} else {
103-
dispatch({
104-
type: actionTypes.SET_VALIDITY,
105-
model,
106-
validity: omit(field.validity, omitKeys),
107-
});
108-
}
109-
};
110-
};
88+
const resetValidity = (model, omitKeys = false) => ({
89+
type: actionTypes.RESET_VALIDITY,
90+
model,
91+
omitKeys,
92+
});
11193

11294
const setFieldsValidity = (model, fieldsValidity, options = {}) => ({
11395
type: actionTypes.SET_FIELDS_VALIDITY,

src/components/control-component.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
140140
this.attachNode = this.attachNode.bind(this);
141141
this.readOnlyValue = isReadOnlyValue(props.controlProps);
142142

143+
this.willValidate = false;
144+
143145
this.state = {
144146
viewValue: props.modelValue,
145147
};
@@ -179,7 +181,7 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
179181

180182
if (fieldValue && !fieldValue.valid) {
181183
const keys = Object.keys(validators)
182-
.concat(Object.keys(errors));
184+
.concat(Object.keys(errors), this.willValidate ? validityKeys : []);
183185

184186
dispatch(actions.setValidity(model, omit(fieldValue.validity, keys)));
185187
}
@@ -319,9 +321,12 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
319321
} = this;
320322

321323
if (!node || !node.willValidate) {
324+
this.willValidate = false;
322325
return null;
323326
}
324327

328+
this.willValidate = true;
329+
325330
const nodeErrors = {};
326331

327332
validityKeys.forEach((key) => {

src/reducers/form-actions-reducer.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,23 @@ export default function formActionsReducer(state, action, localPath) {
194194
}
195195

196196
case actionTypes.RESET_VALIDITY: {
197+
let validity = { ...fieldState.validity };
198+
let errors = { ...fieldState.errors };
199+
200+
if (action.omitKeys) {
201+
action.omitKeys.forEach((key) => {
202+
delete validity[key];
203+
delete errors[key];
204+
});
205+
} else {
206+
validity = initialFieldState.validity;
207+
errors = initialFieldState.errors;
208+
}
209+
197210
fieldUpdates = {
198211
valid: initialFieldState.valid,
199-
validity: initialFieldState.validity,
200-
errors: initialFieldState.errors,
212+
validity,
213+
errors,
201214
};
202215

203216
subFieldUpdates = {

0 commit comments

Comments
 (0)