As mentioned in https://github.com/mozilla-services/react-jsonschema-form/issues/754 ,
We have a working solution for only returning the data actually used in the form. In addition to the problems stated in the reported issue, we also dont want passthrough unused data, even if we provided it originally. We just want to get the data from whatever is in the visible form. As stated, there is no super straight fwd solution, but we think we found a pretty reasonable and not too intrusive solution.
The idea is....
• Add a name
attribute to all of the fields that is the json path to the form data, e.g. foo.bar.1.name
(something that IMO should be there regardless of this solution).
- Checkboxes no longer have randomly generated names
• onSubmit
we can get all of the form elements and make a list of name attributes (the paths).
• use lodash _.pick(formData, [ARRAY_OF_FIELD_NAMES])
to get only the used data and return that as the formData.
We do this if they set the omitUnusedData
prop to true
otherwise it defaults to false
and works as is.
My coworker @codegains helped me out a bit on this too.
==================
A couple things I wish were better...
-
~~I wish there was a more reliable way to generate the paths rather than doing string manipulation on the id
. But it was hard to access all the necessary data at the point of adding the name
attribute.~~
-
~~The idToPath
could fail if the consumer provides a idPrefix
prop with an underscore .~~
@codegains update addresses both of the above. no longer an issue
The bonus of having these paths as the name attributes on the fields is that in the future you could potentially simplify your submit and changes with something like https://github.com/maxatwork/form2js (this is super old but basically it gathers formData based on the name attributes more like traditional html forms).
awaiting response