formik icon indicating copy to clipboard operation
formik copied to clipboard

When submitting, touched object is not created with the right shape and values

Open iustinian-glavan opened this issue 2 years ago • 7 comments

Bug report

Current Behavior

When using similar field components in a nested form object, the touched object is not created accurately at pre-submit phase. Pressing submit does not show the validation errors on the fields.

Expected behavior

When pressing the submit button the touched object should be created accurately and the form should show the validation errors.

Reproducible example

https://codesandbox.io/s/formik-codesandbox-template-forked-h2fxvk?file=/CustomTextField.jsx

Additional context

Seems related to the setNestedObjectValues method and the way it uses the value objects as keys in the WeakMap.

Your environment

Software Version(s)
Formik 2.2.6
React 16.13.1
TypeScript 3.8.3
Browser Chrome Version 101.0.4951.54 (Official Build) (64-bit)
npm/Yarn Yarn 1.22.18
Operating System Ubuntu 20.04

iustinian-glavan avatar May 10 '22 13:05 iustinian-glavan

Got the similar error in Next.js based on Formik.

TypeError: Cannot read property 'touched' of undefined
          at t.n.render (/app/node_modules/formik/dist/formik.cjs.production.min.js:1:17956)

Dandrum avatar May 11 '22 07:05 Dandrum

I don't get any errors, the touched object is not created properly.

iustinian-glavan avatar May 13 '22 06:05 iustinian-glavan

@Dandrum did you get it sorted? seeing the same issue on nextjs but on production build only

mattvb91 avatar May 23 '22 16:05 mattvb91

@Dandrum did you get it sorted? seeing the same issue on nextjs but on production build only

Well i removed the Formik ErrorMessage component and implementet the small part by myself like in the docs.

 {error && (
                <Alert
                  variant=''
                  className={
                    'fade bg-danger text-white alert alert-dismissible show'
                  }
                >
                  {error.message}
                </Alert>
              )}
  • Alert in this case is a bootstrap component ;)

Dandrum avatar May 30 '22 08:05 Dandrum

Yea the error component seems to be causing this issue. Interestingly enough it started happening with the latest nextjs update so not sure if its a formik issue or nextjs

mattvb91 avatar May 30 '22 09:05 mattvb91

I am not using nextjs, which leads me to believe it is a formik issue.

iustinian-glavan avatar Aug 23 '22 07:08 iustinian-glavan

This issue resolved itself for me in a recent renovate minor patch upgrade so I suggest trying the latest version.

mattvb91 avatar Aug 23 '22 07:08 mattvb91