Skip to content

Commit

Permalink
implemented change detection before validation form on blur
Browse files Browse the repository at this point in the history
Solution to server errors being cleared as soon as an input field loses focus (on blur). For issue Expensify#21838
  • Loading branch information
joh42 authored Jul 20, 2023
1 parent 0fe7b58 commit 23096ad
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function Form(props) {
const inputRefs = useRef({});
const touchedInputs = useRef({});
const isFirstRender = useRef(true);
const lastValidatedValues = useRef({...props.draftValues});

const {validate, onSubmit, children} = props;

Expand Down Expand Up @@ -146,6 +147,8 @@ function Form(props) {
setErrors(touchedInputErrors);
}

lastValidatedValues.current = values;

return touchedInputErrors;
},
[errors, touchedInputs, props.formID, validate],
Expand Down Expand Up @@ -300,7 +303,12 @@ function Form(props) {
// web and mobile web platforms.
setTimeout(() => {
setTouchedInput(inputID);
onValidate(inputValues);

// To prevent server errors from being cleared inadvertently, we only run validation on blur if any form values have changed since the last validation/submit
const shouldValidate = !_.isEqual(inputValues, lastValidatedValues.current);
if (shouldValidate) {
onValidate(inputValues);
}
}, 200);

if (_.isFunction(child.props.onBlur)) {
Expand Down

0 comments on commit 23096ad

Please sign in to comment.