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

209
Views
Formik add validation schema to dynamic fields in a form

I am getting a response from api which is a "number" and based on that i have to render those many forms in the ui (which have same fields)

so lets say i am getting "3" from api so what i am doing is that

{Array.apply(null, { length: journey.insuredPersons }).map((item, index) => (
            <OtherPersons formik={formik} index={index} key={index} journey={journey} />
 ))}

so "Otherpersons" component will have to get rendered 3 times

To set initialValues i am running a little code

export const calcInitialValues = journey => {
  if (journey) {
    let insuredPersonsCount = journey.insuredPersons
    let persons = []
    let newValues = []
    for (let i = 1; i < insuredPersonsCount; i++) {
      persons.push(`insuredPersons${[i + 1]}`)
    }
    console.log(persons)
    persons.forEach((item, index) => {
      item = {
        address: '',
        email: '',
        name: '',
      }
      newValues.push(item)
    })
    return newValues
  }
}

so at this point my formik initialValues are like

otherPersons:[
{
name:'',
address:'',
email:'' 
},

{name:'',
address:'',
email:''
},


{name:'',
address:'',
email:''
},
]

so 3 initialValues for 3 persons as of now

to work with the input box inside the rendered components (which are rendered 3 times) i am doing

        export default OtherPersons({formik,index}) {
        return (     <Input
                     size="large"
                     className="input-box full-width-input"
                     {...formik.getFieldProps(`otherPersons[${index}].name`)}
                     />
             )}

which works completely fine and values set all are correct, but i have to add validationschema to my forms for all 3 persons

so how do i set the validationschema for this dynamic keys and show errors after any field is touched (formik.touched && formik.error ? showerror)

Any help is appreciated thanks !

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!