question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sanitizeEmptyValues should not touch properties that do not belong to form fields

See original GitHub issue

reopen 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mrbfrankcommented, Jun 8, 2021

+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 use sanitizeEmptyValues={false} & parse to replace empty values with null…

<SimpleForm sanitizeEmptyValues={false}>
  <TextInput source="myText" parse={value => value ? value : null} />
  <NumberInput source="myNumber" parse={value => value ? value : null} />
  <MyJSONEditor source="myJSON" />
</SimpleForm>

…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 of myJSON outside of <SimpleForm> & un-altered by sanitizeEmptyValues.

0reactions
fzaninottocommented, Jul 21, 2022

No news for some time, closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-admin - Upgrading to v4 - Marmelab
You can specify whether the custom routes it contains should be rendered with the <Layout> or not by setting the noLayout prop. It's...
Read more >
reduxForm(config:Object)
Useful for wizard-type forms where you want to destroy fields as they unmount, but not the form's state. Defaults to false , as...
Read more >
PDF form field properties, Adobe Acrobat
If you select form fields that have different property values, some options in the Properties dialog box are not available.
Read more >
Input file value react
To get the value of an input field in React: Create a state variable to store the input field's value. ... This input...
Read more >
Symfony 3 - Form model data loses property values which are ...
My guess is that PraWorkflowTransitionsType does not contain the fields of PraWorkflowStatesType , so after the second submit it can never have ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found