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

setErrors can not set nested field errors #3369

Open
vishalgheravada opened this issue Oct 7, 2021 · 5 comments
Open

setErrors can not set nested field errors #3369

vishalgheravada opened this issue Oct 7, 2021 · 5 comments

Comments

@vishalgheravada
Copy link

vishalgheravada commented Oct 7, 2021

Bug report

i have a component in form like this: <Field name="detail.firstName" />
I get errors from api as below:
{'detail.firstName': 'some error msg', 'type': 'some error msg'}
setErrros from submit function only sets type msg not firstName.
I can only achieve this by using setFieldError in loop.
Also, I can see formik context is getting updated too.

is there any way I can do that without using a loop?

Your environment

Software Version(s)
Formik 2.2.9
React 16.14.2
TypeScript 3.9.7
Browser Chrome
npm/Yarn 6.14.14
Operating System windows
@mfaheemakhtar
Copy link

@vishalgheravada , can you share a codesanbox link or something similar?

@erend
Copy link

erend commented Dec 8, 2021

I have the same issue.
setErrors({ 'parent.child': 'error msg' }) do nothing.

@sijad
Copy link
Contributor

sijad commented Dec 20, 2021

using object worked for me:

setErrors({ parent: { child: 'error msg' } });

@KatiaZakharina
Copy link

If you are faced with a multi-level nested field, one possible solution is to convert the error object as follows
{ 'a.b.c': 'error msg' } => { a: { b: { c: 'error msg' } } } using Lodash:

function transformDotNotationObject(obj) {
  return _.transform(obj, (result, value, key) => {
    _.set(result, key, value);
  }, {});
}

@charanjit-singh
Copy link

import { ErrorMessage, Field, useFormikContext } from 'formik'
import React from 'react'
import { ServiceFormValues } from '.'

function RentAgreement() {
    const { values, setErrors, setTouched } = useFormikContext<ServiceFormValues>()
    React.useEffect(() => {
        if (values?.configuration?.rentAgreement?.agreementDate) {
            console.log(
                'values?.configuration?.rentAgreement?.agreementDate',
                values?.configuration?.rentAgreement?.agreementDate
            )
            const agreementDate = new Date(values.configuration.rentAgreement.agreementDate)
            const today = new Date()
            if (agreementDate > today) {
                setTouched({
                    configuration: {
                        rentAgreement: {
                            agreementDate: true
                        }
                    }
                }, true)
                setErrors({
                    configuration: {
                        rentAgreement: {
                            agreementDate: 'Agreement date cannot be in future'
                        }
                    }
                })
            }
        }
    }, [values?.configuration?.rentAgreement?.agreementDate, setErrors, setTouched])

    return (
        <div>
            <Field name='configuration.rentAgreement.agreementDate' type='date' className='border border-gray-300 rounded-lg p-2 mt-2' />
            <ErrorMessage name='configuration.rentAgreement.agreementDate' component='div' className='text-red-500 text-sm' />
        </div>
    )
}

export default RentAgreement

Not working for me

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

6 participants