Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering form with array of objects #151

Closed
rcampos87 opened this issue Sep 4, 2017 · 6 comments
Closed

Rendering form with array of objects #151

rcampos87 opened this issue Sep 4, 2017 · 6 comments

Comments

@rcampos87
Copy link

I have the following problem:

I'm trying to render multiple input fields based on index such as:

{values.education.map((item, index) => getInputFields(index))}

where values contains an array of objects as:

education: [ 
{
what: '',
where: '',
description: '',
startDate: '',
endDate: ''
} 
]

and getInputFields(index) renders a set of input fields based on the education index.

The problem im running into is that if i use the default handleChange from formik, the values object i get is:

{
education[0].what: ''
education[0].where: '',
education[0].description: '',
education[0].startDate: '',
education[0].endDate: ''
}

So i decided to implement a custom onChange handler that uses the setFieldValue to manually set education. It works up to a point. Now my errors and touched objects are not working. Errors return the exact same object as before:

{
education[0].what: 'This field is required.'
education[0].where: 'This field is required.',
education[0].description: 'This field is required.',
education[0].startDate: '',
education[0].endDate: ''
}

Is there a special way to handle a form with an array of objects?

Will this feature be implemented anytime soon in formik?

@Andreyco
Copy link
Collaborator

Andreyco commented Sep 4, 2017

Quick solution is to pick unique input names. In this particular situation, I would pick names such as education.{index}.what, education.{index}.where, and so on.

When processing values I would convert from flat list into nested structure.

@rcampos87
Copy link
Author

rcampos87 commented Sep 4, 2017

my input names do use unique names education[{index}].what etc.. it would be a not-so-elegant solution to convert a flat list into a nested structure... but if thats the only way..

if formik would use path instead of field name, itd be so helpful.

@andymac4182
Copy link

Something similar to this would be great https://redux-form.com/7.0.4/docs/api/fieldarray.md/

@stunaz
Copy link

stunaz commented Oct 11, 2017

My input names do use unique name address.label, address.coordinates, I don't event use Array yet, but it would be so awesome to support path to avoid transforming for each form nested Object to Flat List and the other way around from values

@klis87
Copy link
Contributor

klis87 commented Oct 16, 2017

I actually created pull request to remedy this #202 . Do you think it will cover all cases? For both arrays and nested values?

@jaredpalmer
Copy link
Owner

Merged with #207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants