Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation on stale values #3947

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -418,7 +418,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 @@ -549,7 +554,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 @@ -560,7 +565,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 @@ -593,7 +600,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 @@ -678,7 +687,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
Loading