formik: validateField not working
๐ Bug report
When calling validateField the field is not validated
Current Behavior
Here is a repository to checkout and click the button labeled โNext Stepโ which is supposed to validate field โemailโ https://github.com/cyberprodigy/formik-single-field-validation-bug
Expected behavior
When the method validateField is called it should display that the field is required
Reproducible example
Checkout https://github.com/cyberprodigy/formik-single-field-validation-bug execute `npm start`` Observe that no validation is happening
Suggested solution(s)
It should display the error when validateField is invoked. You can trigger focus() followed by blur() to obtain the desired behaviour, but that is not a solution
Additional context
Your environment
| Software | Version(s) |
|---|---|
| Formik | 1.5.8 |
| React | 16.9.0 |
| TypeScript | |
| Browser | |
| npm/Yarn | |
| Operating System | Mac Mojave |
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 18 (7 by maintainers)
Also running into this in a form with a
validationSchemabut novalidate.Looks like if there is no
validatefunction on a field, it just callsPromise.resolve()https://github.com/jaredpalmer/formik/blob/master/src/Formik.tsx#L426Also the full TypeError is
"_this.fields[field].props.validate is not a function"I wanted to do a programmatic validation on a single field (nested object inside of an array) using validationSchema and set/clear field errors based on validation result.
yup.reach(schema, 'path.to.field').validate(fieldValue)was throwing an error:Uncaught TypeError: Cannot read property 'resolve' of undefinedI ended up with this handler as a workaround:
Hope that helps someone with a similar issue:)
P.S. this should not be treated as a substitute for an actual formik validation, see: https://github.com/jaredpalmer/formik/issues/1278#issuecomment-461036355 https://github.com/jaredpalmer/formik/issues/1309
Good find @johnrom! Iโll give this fix a try on the v2 branch this weekend.