Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda