Formik: Executing `validateField` on an FieldArray throws `TypeError: Cannot read property 'validate' of undefined`

Created on 13 Mar 2020  路  5Comments  路  Source: formium/formik

馃悰 Bug report

I consistently get an error TypeError: Cannot read property 'validate' of undefined in the Formik class on line 503:

if (isFunction(fieldRegistry.current[name].validate)) {

It appears that when the field registers, nothing is getting set in the field registry for a validation function. I notice that in the Field code we get our validate function always from props. If I don't pass one (because i'm using Yup), does one get injected for me?

Current Behavior

I have a form that is pretty basic, holds a text field, a boolean, and an array of text fields.

I have broken the form up into parts to show to the user at a given time and I have a function that will accept the field names and the Formik legacyBag and run validateField.

Expected behavior

I think both issues should be handled, I would think I should be able to validate a "parent" field property and it will validate that part of the schema.

I also believe I should be able to validate myArray[i] and it be able to validate that as well, but that might be a stretch goal.

All in all I just need one of these to work, I'm trying to look in the code to assist, but I imagine there are faster people here to help.

Reproducible example

https://codesandbox.io/s/formik-codesandbox-template-p3d29

Suggested solution(s)

I think it might be an issue with how a field registers its validation function. I am currently using the original Field component and not the hook, but it appears that logic is the same.

I think on top of validateField there should be a validateFields where we can give a list of field names to validate.

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 2.1.4
| React | 16.3.0
| TypeScript | 3.7.5
| Browser | All
| npm/Yarn | 6.13.4 (NPM)/1.22.4 (Yarn)
| Operating System | OS X (Mojave)

Most helpful comment

same issue

All 5 comments

same issue

I'm having this issue as well with Yup schema validation. I changed this line to:
if (fieldRegistry.current[name] && isFunction(fieldRegistry.current[name].validate)) {
and it worked, but of course this would need to be fixed in the package.

Is there any update on this?

I raised a PR a while back to fix this issue but it has become stale https://github.com/formium/formik/pull/2239

Merged the latest master into my branch and resolved the conflicts. It is no longer stale.

Was this page helpful?
0 / 5 - 0 ratings