sanitizeEmptyValues should not touch properties that do not belong to form fields
See original GitHub issuereopen https://github.com/marmelab/react-admin/issues/5043
its still reproducable in 3.9.3. It produces really nasty inconsistencies in data when you are using special field with opaque data. It’s so severe that we have to disable it in every form, but that leads to other problems
What you were expecting:
when saving a record, form fields that contain objects as values are not altered. object as values should be treated as opaque and should not be touched. These objects could be representations of a rich-text-field or content editor like react-page
What happened instead:
saving a record may alter object values of form fields because there is a recursive function sanitizeEmptyValues
which tries to sanitize any object tree it finds. This might lead to unexpected [key]: null
in your opaque object.
in particular, sanitizeEmptyValues
only takes in the current record and the old record and does not check which properties in the tree belong to form fields.
A solution would be that sanitizeEmptyValues
in submit
would use the final form api.getRegisteredFields
(api is second unused argument to submit) to check which properties are form fields and would only sanitize these fields and wont be called recursivly. see this code: https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/form/FormWithRedirect.tsx#L95
Steps to reproduce:
- create a custom form field that contains an object tree (with arrays)
- create a record
- update this record in a way that a nested property of this custom form field is changed
- sanitizeEmptyValues will then go recursilvy through your object value and might add [key]: null properties
Environment
- React-admin version: 3.9.3
- Last version that did not exhibit the issue (if applicable): 3.5.3 (actually it already exists there, but its not that severe because arrays are unaltered
- React version: 16.13.1
- Browser: chrome
- Stack trace (in case of a JS error):
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top GitHub Comments
+1 on this issue. I agree that form values that are objects should not be modified by
sanitizeEmptyValues
, I’ve added a JSON editor to my form & any key/value pairs I attempt to delete from the object are re-added as{"foo": null}
instead they should be omitted from the object. My temporary solution is to usesanitizeEmptyValues={false}
&parse
to replace empty values with null……but this is cumbersome & the best solution is for
sanitizeEmptyValues
to treat objects in forms as opaque as @macrozone suggested.An alternative solution could be for something like
sanitizeEmptyValues
to be available on individual fields, instead of the entire form. In that case I’d disable it directly on my<MyJSONEditor />
component.FWIW I also tried using
transform
but was unable to access the raw form field value ofmyJSON
outside of<SimpleForm>
& un-altered bysanitizeEmptyValues
.No news for some time, closing.