Skip to content
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 @@ -595,7 +602,9 @@ export function useFormik<Values extends FormikValues = FormikValues>({
const willValidate =
shouldValidate === undefined ? validateOnChange : shouldValidate;
return willValidate
? validateFormWithHighPriority(setIn(state.values, field, resolvedValue))
? validateFormWithHighPriority(
setIn(stateRef.current.values, field, resolvedValue)
)
: Promise.resolve();
}
);
Expand Down Expand Up @@ -680,7 +689,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
41 changes: 41 additions & 0 deletions packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1547,4 +1547,45 @@ describe('<Formik>', () => {

expect(InitialValuesWithNestedObject.content.items[0].cards[0].desc).toEqual('Initial Desc');
});

it('Should run validation on actual values when tirggering setFieldTouched after setFieldValue', async () => {

const validationShema = {
validate: jest.fn(() => Promise.resolve({})),
}

render(
<Formik
initialValues={InitialValues}
onSubmit={noop}
validationSchema={validationShema}
>
{formikProps => (
<input
data-testid="desc-input"
value={formikProps.values.name}
onChange={e => {
formikProps.setFieldValue('name', e.target.value);
formikProps.setFieldTouched('name', true);
}}
/>
)}
</Formik>
);

const input = screen.getByTestId('desc-input');

fireEvent.change(input, {
target: {
value: 'New Value',
},
});

await waitFor(() => {
expect(validationShema.validate).toHaveBeenLastCalledWith(
{ age: 30, name: 'New Value' },
{ abortEarly: false, context: { age: 30, name: 'New Value' } }
);
});
});
});