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

134
Views
Setting Error Messages Around the Applicable Form Elements in Loop

I have this function and I need error message to be applied on around particular element; where validation fails.

Currently the SetError message is applied all the form elements in the loop.

async function onUserInput(element, value) {
console.log(element);
if (typeof element === 'string') {
  let pageUserInputCopy = [...pageUserInput];
  let index = pageUserInputCopy.findIndex((obj) => obj.key === element);
  let itemCopy = { ...pageUserInputCopy[index] };

  itemCopy['userInput'] = value;
  pageUserInputCopy[index] = itemCopy;

  setPageUserInput(pageUserInputCopy);
}
if (await isValid(value)== false) {
  setErrorMessage('Collection Container ID already exists!');
  setHasError(true);
}

}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that your Promise will get evaluated as undefined if it's written inside an if statement because it is not being evaluated properly.

This should do the trick:

async function onUserInput(element, value) {
  console.log(element);
  if (typeof element === "string") {
    let pageUserInputCopy = [...pageUserInput];
    let index = pageUserInputCopy.findIndex((obj) => obj.key === element);
    let itemCopy = { ...pageUserInputCopy[index] };

    itemCopy["userInput"] = value;
    pageUserInputCopy[index] = itemCopy;

    setPageUserInput(pageUserInputCopy);
  }
  const valid = await isValid(value);
  if (!valid) {
    setErrorMessage("Collection Container ID already exists!");
    setHasError(true);
  }
}
about 4 years ago · Juan Pablo Isaza Report
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!