Formik: Program errors within validate are lost

Created on 20 Nov 2018  路  3Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

When an exception is being thrown in the validate function, the exception-object is being treated as a validation result. Since the exception does not contain any fields of the form, the form is considered valid.

Expected behavior

The error should be rethrown and if not otherwise caught, the program should crash.

Reproducible example

https://codesandbox.io/s/7mryz7opqx

Suggested solution(s)

Using reject() to indicate that there is an error is imho a bit of a poor design: The goal of the validate method should be, to find validation errors. The reject method of a promise however should be used when the promise cannot be completed, for example because of an error. So a successful validation should end up in a resolve of the promise, as the function core functionality and the expected result were successfully produced.

I'd recommend to change the api here, so that a valid form state is indicated by the state of the returned object. eg. an empty object indicates that there is no validation error.

Additional context

This problem described in here is especially problematic sinc using the validate function asynchronously is usually the case when communicating with a server for complexer validations. That szenario is likely to fail because of external issues.

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 1.2.0
| React | 16.5.0
| TypeScript |
| Browser | chrome 70
| npm/Yarn | npm
| Operating System | macOS Sierra, Windows 10

Bug stale

Most helpful comment

@jaredpalmer Do you need any help to fix this issue? We find this line https://github.com/jaredpalmer/formik/blob/0e459bce5f88bc60681f81ffe13df81982a723ab/src/Formik.tsx#L296 is the root cause. What is your suggestion?

All 3 comments

@jaredpalmer Do you need any help to fix this issue? We find this line https://github.com/jaredpalmer/formik/blob/0e459bce5f88bc60681f81ffe13df81982a723ab/src/Formik.tsx#L296 is the root cause. What is your suggestion?

@PeerHartmann imho you are correct. Indicating validation errors in any async validation should be passed to formik via resolve. There are several bugs and questions that come up, i.e. https://github.com/jaredpalmer/formik/issues/966, https://github.com/jaredpalmer/formik/issues/1141

This would be a breaking change.

@jaredpalmer as this would be a breaking change, whats your opinion on this? I would also jump in to help working on this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmonty picture pmonty  路  3Comments

ancashoria picture ancashoria  路  3Comments

outaTiME picture outaTiME  路  3Comments

sibelius picture sibelius  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments