Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!