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

113
Visualizações
Validate multiple fields with different index using react hook form

I'm trying to validate some fields that are all part of my field array. They have same names, but they have a different array index.

                <Select
                {...register(`documents.${index}.language`, {
                    validate: (v) => getValues(v),
                })}
                sx={{ textAlign: 'center' }}
            >
                {availableLanguages
                    ?.sort((a) => (a.name === 'pl' ? -1 : 1))
                    // sorting so pl is default value
                    .map((language) => (
                        <option key={language.name} value={language.name}>
                            {language.name.toUpperCase()}
                        </option>
                    ))}
            </Select>

I want to validate by getting all the language values and check if they are all unique.

The output array creates an object for each field the user has decided to fill in and returns a language value in that object + some others.

[{language: 'en'},{language: 'en'}, {language: 'br'}]

I want to validate this, preferably before submit, meaning that it would have to be done in the validate function of the field.

Other way, I could also validate it on submit, however I'm also not sure how to do this efficiently.

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

0

For anyone in the future this is the way I have been able to go around this validation. A simple for loop, there are other ways but they don't actively get the field value unless kept in state.

 <Select
                        {...register(`documents.${index}.language`, {
                            validate: (v) => {
                                for (let x = 0; x < index; x++) {
                                    if (v === getValues(`documents.${x}.language`)) return false;
                                }
                            },
                        })}
                        sx={{ textAlign: 'center' }}
                    >
                        {sortLanguages(availableLanguages).map((language) => (
                            <option key={language.name as string} value={language.name as string}>
                                {language.name.toUpperCase()}
                            </option>
                        ))}
                    </Select>
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