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

bug: submitForm returns a resolved promise even when validation failed #3280

Open
guijiangheng opened this issue Jul 21, 2021 · 5 comments · May be fixed by #3995
Open

bug: submitForm returns a resolved promise even when validation failed #3280

guijiangheng opened this issue Jul 21, 2021 · 5 comments · May be fixed by #3995

Comments

@guijiangheng
Copy link

https://github.com/formium/formik/blob/903c1101240632c84fa36c8991ea9fd03b25f1cf/packages/formik/src/Formik.tsx#L785

Document says submitForm return a promise, which will reject when validation failed. But due to the line of code I mentioned, It wont throw when validation failed.

Wish this bug fixed as soon as possible.

@djejaquino
Copy link

Duplicate of #1580

@olignyf
Copy link

olignyf commented Oct 6, 2021

Actually a duplicate of #1580, #1810 and #1904.
Maybe a unit test on that use case would be appropriate ;)

@olignyf
Copy link

olignyf commented Oct 6, 2021

This is my workaround. Instead of using catch(), use then() and check for !formikProps.isValid

formikProps.submitForm().then(() => {
   if (!formikProps.isValid && formikProps.errors) {
      // the form did not submit
       console.log('errors', formikProps.errors);
    }
});

Note that it might not be accurate in all scenarios. A proper fix would be needed. Or wait for v3 formikProps.getState().isValid.

@johnrom
Copy link
Collaborator

johnrom commented Oct 6, 2021

@olignyf the user could technically change the form during submit (unless you prevent this) if you use an async request, cause it to be valid or invalid even though the submission was the opposite, and the check above wouldn't work in v3. The validation result would have to be passed to onSubmit's promise to be accurate.

The check could also not work in v2 if validation was not completed by the time the user submitted (e.g. async validation)

@olignyf
Copy link

olignyf commented Aug 29, 2022

@johnrom thanks for your reply.
What workaround do you suggest or improvements on my code above?

How come you think that

The check could also not work in v2 if validation was not completed by the time the user submitted (e.g. async validation)

In my code I used the .then() async promise. Are you really saying that then() could fire while the validation have not ran yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants