Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

428
Visualizações
Always get undefined when using Yup.array().when() with two arrays

I have two arrays and I want at least one of them to be filled before submitting

My config:

initialValues={{
    images: [],
    current_images: [],
}}
validationSchema={Yup.object().shape(
    {
        images: Yup.array().when("current_images", {
            is: (current_images) => current_images.length === 0,
            then: Yup.array().min(1, "Pick at least one image"),
            otherwise: Yup.array(),
        }),
        current_images: Yup.array().when("images", {
            is: (images) => images.length === 0,
            then: Yup.array().min(1, "Pick at least one image"),
            otherwise: Yup.array(),
        }),
    },
    [["current_images", "images"]]
)}

Version:

"formik": "^2.2.9",
"yup": "^0.32.11"

Here is the error I got after clicking submit button: enter image description here

How can I fix this problem ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found an alternative solution that using .test() instead of .when()

initialValues={{
    images: [],
    current_images: [],
}}
validationSchema={Yup.object().shape(
    {
        images: Yup.array().test(
            "images_required", // test name
            "Pick at lease one image", // error message
            function (item) {
                // item is the current field (images in this case)
                return item.length > 0 || this.parent.current_images.length > 0;
            }
        }),
        current_images: Yup.array().test(
            "current_images_required",
            "Pick at lease one image",
            function (item) {
                return item.length > 0 || this.parent.images.length > 0;
            }
        }),
    }
)}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda