jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.88k stars 2.79k forks source link

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

Open guijiangheng opened 3 years ago

guijiangheng commented 3 years ago

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 commented 3 years ago

Duplicate of #1580

olignyf commented 2 years ago

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

olignyf commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

@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?