React-admin: ReferenceInput with allowEmpty has emptyValue '', not null

Created on 2 Apr 2019  路  9Comments  路  Source: marmelab/react-admin

What you were expecting:
Using any of these:

<ReferenceInput source="resourceId" reference="resource" resettable>
  <SelectInput optionText="name" />
</ReferenceInput>
<ReferenceInput source="resourceId" reference="resource" resettable allowEmpty>
  <SelectInput optionText="name" />
</ReferenceInput>



md5-48970796820253497a9fd010fc332b1e



<ReferenceInput source="resourceId" reference="resource" resettable allowEmpty emptyValue={null}>
  <SelectInput optionText="name" />
</ReferenceInput>

should result in the value being null when the select is empty, as per the documentation:
https://marmelab.com/react-admin/Inputs.html#referenceinput

What happened instead:
The value is an empty string '', which crashes as the dataProvider is (correctly) expecting either a integer or null.

Environment

  • React-admin version: 2.8.4
  • React version: 16.8.5
  • Browser: Chrome
bug

Most helpful comment

Hi @fzaninotto,

I totally agree with what you said, but there's still two gotchas:

  • If the default empty value of ReferenceInput is the empty string, then the documentation is wrong, as it says it is null and even shows the emptyValue being used with an empty string (which wouldn't make sense were it the default).
  • The emptyValue prop would solve my problem, the problem is it doesn't seem to work. Check my third code snippet above, I explicitly pass null to the emptyValue prop and I still get an empty string back...

All 9 comments

Hi @fvieira,

There is no good value for the empty choice that works in all cases. Whether you need a number or a string, you'll want different default values. So ReferenceInput chose one (the empty string) which is the most common, and lets you change it when you need. You can choose the value of the empty choice with the emptyValue prop, as explained in the documentation.

Hi @fzaninotto,

I totally agree with what you said, but there's still two gotchas:

  • If the default empty value of ReferenceInput is the empty string, then the documentation is wrong, as it says it is null and even shows the emptyValue being used with an empty string (which wouldn't make sense were it the default).
  • The emptyValue prop would solve my problem, the problem is it doesn't seem to work. Check my third code snippet above, I explicitly pass null to the emptyValue prop and I still get an empty string back...

Then it means that you're right and I'm wrong. I'm reopening this and marking it as a bug. Thanks for the report!

Well, can't we both be right? Your arguments were on spot! ;)
Just don't forget to actually reopen the ticket, cause you added the bug label but the ticket is still closed...

I'm having the same issue, any update?

even with emptyValue={null} it crashes the dataprovider, in my case https://github.com/Weakky/ra-data-opencrud/

tested with react-admin 3.0.0-alpha.4

edit: sorry, https://github.com/marmelab/react-admin/issues/3087#issuecomment-479380792 already mentioned that this is not fixed.

I debugged that a bit and it seems that its not only a matter of emptyValue.

The referenceField will use useGetMany with ids=[""] when it has no value, this is called from useReference({id: ""}). That is semantically wrong. When no item is selected, it should not try to fetch a referenced record. A SelectField should know whether an item of its options is selected or not. At the moment it just says: 'yes, i got a value: ""'.

I don't think that this is solved properly with emptyValue, at least not for SelectFields. These should now their option and know whether a value is selected or not. Encoding that with an empty string is probably not a good idea. null would be semantically better, but still not ideal. It should have some property "hasNoValue" or so to indicated that it has nothing selected

error is still there on react-admin 3.0.1.

I also noticed, that if i don't touch the field, it now sends an empty string "" to the dataprovider instead of null.

When i touch the field and select the empty value there, i get a different error:

TypeError: Cannot convert undefined or null to object

this occures sanitizeEmptyValues, it will recursivly sanitize something like this data:

Bildschirmfoto 2019-11-25 um 17 34 25

notice the parentPage: undefined and parentPage.id: undefined

it will then call the sanitizeFunction with initialValues["parentPage"], which is undefined. So calling Object.keys(undefined) will crash. It should have a check there

I can't reproduce the error on master anymore, I believe it was accidentally fixed by #4103 or #4082. Feel free to reopen if the error is still there once 3.0.3 is released.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alukito picture alukito  路  3Comments

fzaninotto picture fzaninotto  路  3Comments

aserrallerios picture aserrallerios  路  3Comments

9747749366 picture 9747749366  路  3Comments

Dragomir-Ivanov picture Dragomir-Ivanov  路  3Comments