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

187
Views
How to get the relevant error message when using React-hook-forms in a loop ,

I have a loop that creates input fields. I managed to make it validate using react-hook-form and yup. but I cannot get the error message because of a variable issue...

{Object.getOwnPropertyNames(row)?.map((head) => {
                    return (
                      <div key={head} className="row my-2">
                        <div className="col-5">{createHeads(head)}</div>
                        <div className="col-7">{createInputs(head)}</div>
                        <div className="col-12">
                          <small className="text-danger">{errors.head?.message}</small>
                        </div>
                      </div>
                    );
                  })}

So head is a variable that contains the name of the input field. but it's not using that variable . is there a way around it?

--Edit

function createInput(head){
  return <input type="text" name="head" {...register(head)} />
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

{Object?.getOwnPropertyNames(row)?.map((head) => {
                    const error = Object.getOwnPropertyNames(errors).filter((error) => error == head);

                    return (
                      <div key={head} className="row my-2">
                        <div className="col-5">{createHeads(head)}</div>
                        <div className="col-7">{createInputs(head)}</div>
                        <div className="col-12">
                          <small className="text-danger">{errors[error]?.message}</small>
                        </div>
                      </div>
                    );
                  })}

Update im just dumb edit :

{Object?.getOwnPropertyNames(row)?.map((head) => {
                    return (
                      <div key={head} className="row my-2">
                        <div className="col-5">{createHeads(head)}</div>
                        <div className="col-7">{createInputs(head)}</div>
                        <div className="col-12">
                          <small className="text-danger">{errors[head]?.message}</small>
                        </div>
                      </div>
                    );
                  })}
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!