Can my empty TextInput value be sent to the DataProvider as empty string instead of converted to null?
Asked Answered
J

3

5

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?

Jesse answered 11/7, 2020 at 19:18 Comment(4)
How do you clear the values at runtime?Rebba
@MiguelAraya with empty strings replacing null valuesJesse
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 codeJesse
P
6

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.

Peevish answered 28/7, 2020 at 6:11 Comment(0)
C
1

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

Cointon answered 2/4, 2021 at 5:53 Comment(0)
H
0

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

Hutson answered 23/7, 2020 at 17:52 Comment(1)
That accomplishes something differentJesse

© 2022 - 2024 — McMap. All rights reserved.