Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validateField does not validate nested field #3252

Open
jansedlon opened this issue Jun 21, 2021 · 15 comments
Open

validateField does not validate nested field #3252

jansedlon opened this issue Jun 21, 2021 · 15 comments

Comments

@jansedlon
Copy link
Contributor

Bug report

Current Behavior

As stated in the title, validateField does not validate nested fields like user.name.

Expected behavior

It should set the correct error.

Reproducible example

You can switch between the commented lines. This shows the difference between usage of validateField with a not nested field and a nested one.

https://codesandbox.io/s/naughty-frost-013fk?file=/src/App.js

Suggested solution(s)

It took me soooo many hours when I encountered it till I realized that the bug might actually be in the Formik itself. I found that when the field error is set, it should use getIn(error, name) instead of error[name]. I'm gonna create a PR with a fix and a test.

diff --git a/packages/formik/src/Formik.tsx b/packages/formik/src/Formik.tsx
index d952a90..8b2d1b7 100755
--- a/packages/formik/src/Formik.tsx
+++ b/packages/formik/src/Formik.tsx
@@ -513,7 +513,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
         .then((error: any) => {
           dispatch({
             type: 'SET_FIELD_ERROR',
-            payload: { field: name, value: error[name] },
+            payload: { field: name, value: getIn(error, name) },
           });
           dispatch({ type: 'SET_ISVALIDATING', payload: false });
         });

Your environment

Software Version(s)
Formik 2.2.6
React 17.0.1
TypeScript 4.2.4
Browser Any
npm/Yarn npm 7.16.0
Operating System MacOS 11 Big Sur
@jansedlon
Copy link
Contributor Author

jansedlon commented Jun 21, 2021

Made a PR. Hopefully everything's correct, this is my first PR to Formik

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jul 22, 2021
@jansedlon
Copy link
Contributor Author

I don't want this to close, this one is important

@johnrom johnrom removed the stale label Jul 22, 2021
@silberistgold
Copy link

I have the same issue at the moment. I am trying to use validateField on an indexed field in an array like so: validateField("friends.2.name") and it does nothing.
As fas as I can tell your fix would enable this as well, correct @jansedlon ?
Can someone please review, merge and release?

@jansedlon
Copy link
Contributor Author

@silberistgold Havent tried that with arrays but if getIn can deal with arrays then I'm pretty confident that it should work as well

@mckinleymedia
Copy link

+1

2 similar comments
@BillTmn
Copy link

BillTmn commented Aug 29, 2021

+1

@vancy-pants
Copy link

+1

@lancemcmuffin
Copy link

Would love to see this merged

@alicansoysal
Copy link

+1

@CSDev0
Copy link

CSDev0 commented Feb 23, 2022

need this!!! how its going?

@sirkeng
Copy link

sirkeng commented Feb 23, 2022

I also just get this problem in getIn

@CSDev0
Copy link

CSDev0 commented Feb 23, 2022

actually the only workaround that i found is validate the entire form instead of each field. its not very UX friendly anyways.

@lokriet
Copy link

lokriet commented Jun 23, 2022

any ideas why this is not merged? This is really needed in an already painful workaround for formik not allowing validations of single field on blur, only the entire form. Now looks like we're having to do a workaround on a workaround, and it could be easily avoided with this one liner :(

@lancemcmuffin
Copy link

Note: it appears this bug was fixed as of release 2.2.10, via #3783

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests