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

212
Vistas
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
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