Skip to content

Commit

Permalink
Fix validation on stale values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jestermaxrko committed Jan 25, 2024
1 parent 0f960aa commit bc841a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-walls-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': patch
---

Fixed validation on stale values
19 changes: 14 additions & 5 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ export function useFormik<Values extends FormikValues = FormikValues>({
dispatchFn();
}
},
[props.initialErrors, props.initialStatus, props.initialTouched, props.onReset]
[
props.initialErrors,
props.initialStatus,
props.initialTouched,
props.onReset,
]
);

React.useEffect(() => {
Expand Down Expand Up @@ -548,7 +553,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
const willValidate =
shouldValidate === undefined ? validateOnBlur : shouldValidate;
return willValidate
? validateFormWithHighPriority(state.values)
? validateFormWithHighPriority(stateRef.current.values)
: Promise.resolve();
}
);
Expand All @@ -559,7 +564,9 @@ export function useFormik<Values extends FormikValues = FormikValues>({

const setValues = useEventCallback(
(values: React.SetStateAction<Values>, shouldValidate?: boolean) => {
const resolvedValues = isFunction(values) ? values(state.values) : values;
const resolvedValues = isFunction(values)
? values(stateRef.current.values)
: values;

dispatch({ type: 'SET_VALUES', payload: resolvedValues });
const willValidate =
Expand Down Expand Up @@ -592,7 +599,9 @@ export function useFormik<Values extends FormikValues = FormikValues>({
const willValidate =
shouldValidate === undefined ? validateOnChange : shouldValidate;
return willValidate
? validateFormWithHighPriority(setIn(state.values, field, value))
? validateFormWithHighPriority(
setIn(stateRef.current.values, field, value)
)
: Promise.resolve();
}
);
Expand Down Expand Up @@ -677,7 +686,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
const willValidate =
shouldValidate === undefined ? validateOnBlur : shouldValidate;
return willValidate
? validateFormWithHighPriority(state.values)
? validateFormWithHighPriority(stateRef.current.values)
: Promise.resolve();
}
);
Expand Down

0 comments on commit bc841a2

Please sign in to comment.