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

error message doesn't seems to work in usefieldarray #90

Open
Bat-m opened this issue Feb 9, 2022 · 3 comments
Open

error message doesn't seems to work in usefieldarray #90

Bat-m opened this issue Feb 9, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@Bat-m
Copy link

Bat-m commented Feb 9, 2022

the error dont appear in error message
not sure if this is an issue but cant figure out an other way

const { fields } = useFieldArray({
    name: nameForm,
    ...methods.control
  });

fields.map((field: FieldValues, index: number) => {
            const id = '${nameForm}.${index}.checkbox'

            return (
                <input
                  {...methods.register(id, { required: required })}
                />
              </div>
            );
          })

<ErrorMessage
        errors={errors}
        name={nameForm}
        render={({ message }) => {
          return <div className="text-red-500 text-sm">{message}</div>
        }}
      />

Capture d’écran 2022-02-09 à 10 29 43

@bluebill1049 bluebill1049 added the bug Something isn't working label Nov 9, 2022
@dd8888
Copy link

dd8888 commented Jan 31, 2023

I got it working by doing this:

 <ErrorMessage
        errors={methods.formState.errors}
        name={`${name}.root`} <-- This is important
        render={({ message, messages }) => {
          return (
            <span >
              {message}
            </span>
          );
        }}
      />

@simonpeters
Copy link

has anyone got this fixed? above solution doesn't work for me.

@iamjaydev
Copy link

For my case, yup resolver was causing the issue. (I was using Custom Hook with Resolver Shown in this docs )

To address the problem,
I switched to using the @hookform/resolvers package.

However, I also discovered that using square brackets instead of a dotted name can resolve the issue.

For example, instead of test.${index}.lasttname, you can use test[${index}].lastName when defining the name property for a Controller component.
This may be a simpler solution for some cases.

This works because the square brackets align with the name that Yup uses.

Hope it helps someone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants