Search code examples
react-admin

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


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?


Solution

  • 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.