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

99
Visualizações
Reactjs validation using Joi

The validation of JOI seems fine for me, scenario: when i tried to click submit without the value or selecting any categories it didnt submit and an error message appear, but when i tried to select what i want is that the error message will disappear and when the user click submit again it will success. thats the current behavior of this code, the Autocomplete is multiple selection

how can i achieve this : what i want is that the error message will disappear and when the user click submit again it will success??

export const VALIDATION_SCHEMA = {
  categories: Joi.array()
    .label('Categories')
    .items(
      Joi.object({
        name: Joi.string().required(),
        value: Joi.string().required(),
      }).required().messages({
        'string.empty': '"Categories" is invalid'
      }),
    ),
}

//

  const onHandleChangeInputCategories = (field, value) => {
    console.log(value) // please see below
    const _categories = value.map((name) => name)
    console.log("value: ", _categories)
    onHandleInputValidation(
      'categories',
      _categories);
  };

  const onHandleInputValidation = (field, value) => {
    try {
      Joi.assert(value, _.get(VALIDATION_SCHEMA, field));
      setFormError(_.omit(formError, field));
      return { error: false, valid: true };
    } catch (err) {
      return { error: err.message, valid: false };
    }
  };

<Autocomplete
     onChange={(event, value) => {
                onHandleChangeInputCategories('categories', value);
              }}
     renderInput={(params) => (
          <TextField
                  {...params}
                  label={'Categories'}
                  error={Boolean(formError.categories)}
                  sx={{
                    flexWrap: 'wrap',
                  }}
                  key="tf_categories"
           />
     )}

/>
<FormHelperText
      error={Boolean(formError.categories)}>
      {formError.categories}
</FormHelperText>

console result

enter image description here

about 4 years ago · Juan Pablo Isaza
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