`AutocompleteInput` `emptyValue` prop is ignored
What you were expecting:
To be able to change the emptyValue of a AutocompleteInput to anything (especially to null), and this value to be sent to the dataProvider when I don't select anything from the AutocompleteInput
What happened instead:
The emptyValue is ignored, and the dataProvider always receives ''.
Steps to reproduce:
- Go to https://stackblitz.com/edit/github-xyrrnf?file=src/posts/PostCreate.tsx
- Make sure to log in as Admin (admin/password)
- Create a new Post, where you have added an Author but left the User field empty
- Notice the dataProvider's create function is called, and
authors[0].user_id=''instead of expectedauthors[0].user_id='emptyValue'
Related code:
PostCreate.tsx line 156
Other information:
This issue was discovered while trying to set emptyValue to null. We should also make sure this is allowed and does not get replaced by '' because it is falsy.
Environment
- React-admin version: 4.2.7
- Last version that did not exhibit the issue (if applicable):
- React version: 18
- Browser: chrome
- Stack trace (in case of a JS error):
or you can use validate={required()} to make it highlight when not selected a value
@tulequ This will not help if the field is not required.
This applies to the SelectInput as well.
In your codesandbox, if you change the AutocompleteInput by a SelectInput, you get the same result.
as a temporary workaround you can use the transform property of the <Edit/>, <Create/> or <SaveButton/> component, see this section from docs.
It can look like this:
<Edit
title="Edit the author"
transform={author => author.user_id === '' ? {...author, user_id: null} : author}
>
<SimpleForm>
<TextInput source="name"/>
<ReferenceInput source="user_id" reference="users">
<AutocompleteInput optionText="name"></AutocompleteInput>
</ReferenceInput>
</SimpleForm>
</Edit>