I'm using a <TextInput>
in a <SimpleForm>
and when I clear out the value at runtime, react-admin sets the property's value to null
when it sends the values to the DataProvider. I would like to store empty strings instead of null. Is that possible? If so, how?
Can my empty TextInput value be sent to the DataProvider as empty string instead of converted to null?
Asked Answered
How do you clear the values at runtime? –
Rebba
@MiguelAraya with empty strings replacing null values –
Jesse
I was expecting some code, inside a FormDataConsumer? –
Rebba
@MiguelAraya The value is being cleared out by the user (e.g. by deleting the text in the textbox), not by calling code –
Jesse
You can use react-final-form parse
prop for that (see https://final-form.org/docs/react-final-form/types/FieldProps#parse):
<TextInput
source="author.name"
parse={value => value}
/>
The default will convert empty string to undefined
(see https://final-form.org/docs/final-form/faq#why-does-final-form-set-my--field-value-to-undefined) and react-admin convert those undefined values to null.
Current version of react-admin
allows you to change this behavior with sanitizeEmptyValues
prop:
export const PostEdit = (props) => (
<Edit {...props}>
<SimpleForm sanitizeEmptyValues={false}>
<TextInput source="title" />
<JsonInput source="body" />
</SimpleForm>
</Edit>
);
More details in official docs: https://marmelab.com/react-admin/CreateEdit.html#setting-empty-values-to-null
You can try to add initialValue=""
prop into it, like
<TextInput initialValue="" />
Which will override it into an empty string when it is null
or undefined
That accomplishes something different –
Jesse
© 2022 - 2024 — McMap. All rights reserved.