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(2172)!: validateOnMount initialize isValid to false until validation performed #3984

Open
wants to merge 1 commit 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/sour-cats-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': major
---

validateOnMount initialize isValid to false until validation performed
23 changes: 14 additions & 9 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
const initialTouched = React.useRef(props.initialTouched || emptyTouched);
const initialStatus = React.useRef(props.initialStatus);
const isMounted = React.useRef<boolean>(false);
const validated = React.useRef<boolean>(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your move to add a ref here is right, but we should be running validation synchronously on mount if validateOnMount is set such that validation has occurred within the first render pass and there is no potential for the boolean to flip either way.

Copy link
Author

@anonkey anonkey Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should i run validation synchronously since it may be async ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@quantizor any hint ? this cause annoying UI issue for me

const fieldRegistry = React.useRef<FieldRegistry>({});
if (__DEV__) {
// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down Expand Up @@ -314,6 +315,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
props.validationSchema ? runValidationSchema(values) : {},
props.validate ? runValidateHandler(values) : {},
]).then(([fieldErrors, schemaErrors, validateErrors]) => {
validated.current = true;
const combinedErrors = deepmerge.all<FormikErrors<Values>>(
[fieldErrors, schemaErrors, validateErrors],
{ arrayMerge }
Expand Down Expand Up @@ -949,15 +951,18 @@ export function useFormik<Values extends FormikValues = FormikValues>({
);

const isValid = React.useMemo(
() =>
typeof isInitialValid !== 'undefined'
? dirty
? state.errors && Object.keys(state.errors).length === 0
: isInitialValid !== false && isFunction(isInitialValid)
? (isInitialValid as (props: FormikConfig<Values>) => boolean)(props)
: (isInitialValid as boolean)
: state.errors && Object.keys(state.errors).length === 0,
[isInitialValid, dirty, state.errors, props]
() => {
if (!validated.current && validateOnMount) return false
if (typeof isInitialValid !== 'undefined') {
if (dirty) return state.errors && Object.keys(state.errors).length === 0;

return isInitialValid !== false && isFunction(isInitialValid)
? (isInitialValid as (props: FormikConfig<Values>) => boolean)(props)
: (isInitialValid as boolean)
}
return state.errors && Object.keys(state.errors).length === 0
},
[isInitialValid, dirty, state.errors, props, validated.current]
);

const ctx = {
Expand Down
13 changes: 13 additions & 0 deletions packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ describe('<Formik>', () => {
expect(props.submitCount).toBe(0);
});

it('should initialize isValid to false if validateOnMount until validation performed', async () => {
const validate = jest.fn(() => Promise.resolve());

const { getProps } = renderFormik({ validateOnMount: true, validate });
expect(getProps().isValid).toBe(false);

await waitFor(() => {
expect(validate).toHaveBeenCalledTimes(1);
});

expect(getProps().isValid).toBe(true);
});

describe('handleChange', () => {
it('updates values based on name attribute', () => {
const { getProps, getByTestId } = renderFormik<Values>();
Expand Down
Loading