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

Conditional validation between different objects #2239

Open
clouds83 opened this issue Aug 8, 2024 · 1 comment
Open

Conditional validation between different objects #2239

clouds83 opened this issue Aug 8, 2024 · 1 comment

Comments

@clouds83
Copy link

clouds83 commented Aug 8, 2024

I'm creating a pretty complex form using RHF and Yup for validation, and I need to add a conditional validation in a field, based in a field that lives outside the object of this field.

I searched extensively for a solution, but I couldn't find one. For conditional validation within the same level works fine.

securityDetails: yup.array().of(
  yup.object().shape({
    listingDetails: yup.object().shape({
      investorNoteEn: yup.string(),
      investorNoteFr: yup.string().when('investorNoteEn', {
        is: (value) => !!value,
        then: (value) => value.required('Please complete this field in both English and French before submitting.'),
        otherwise: (value) => value.optional(),
      }),
    }),

    marketCapitalization: yup.object().shape({
      totalEquity: yup.string().when('investorNoteEn', {
        is: (value) => {
          console.log(value)
          return !!value
        },
        then: (value) => value.required('Total equity is required'),
        otherwise: (value) => value.optional(),
      }),
    }),
  }),
),

The first object, listingDetails, has a conditional validation that works fine, but the second that has pretty much the same validation conditioned by the same investorNoteEn field, doesn't work.

I tried the following approaches:

  • Adding the $, like this: "$investorNoteEn"
  • Creating a path, like this: "securityDetails.listingDetails.investorNoteEn" and "listingDetails.investorNoteEn"
  • Also tried mixing and matching the two previous methods, adding the $

Does anyone know a way to achieve that?

Thanks in advance.

@LeonardoSousa20
Copy link

Hi, I'm in the same situation, did you manage to find a solution to this problem?

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

No branches or pull requests

2 participants