react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Empty (null/undefined) properties are not submitted in SelectArrayInput, SelectInput, DateInput

Open yonitou opened this issue 8 months ago • 3 comments

What you were expecting: When I have a SimpleForm, every key/value couple that is present in the form must be present in the final object when submitting it

What happened instead: For some kind of inputs : DateInput, SelectArrayInput, SelectInput, if the input has not been touched (is not dirty), the key is not present in the final object. I tried to use parse, format, sanitizeEmptyValues, transformOptions, defaultValue, none of them worked. I don't know in which version the issue appeared but I know that it was working before.

Related code:

<SelectInput
	choices={[{id: 1, name: "test"}, {id: 2, name: "test2}]}
	label="resources.usages.fields.doseIsCheck"
	source="doseIsCheck"
/>

When using a simple SelectInput like this one, if I'm not "touching" the input, the key doseIsCheck won't be present when submitting the form. I didn't customize any parent component like Create or SimpleForm. It's really boiler plate code

Environment

  • React-admin version: 6.5.4
  • Last version that did not exhibit the issue (if applicable): Hard to say :/
  • React version: 18
  • Browser: Safari

yonitou avatar Apr 16 '25 16:04 yonitou

Thanks for reporting this. Please provide a sample application showing the issue, preferably a sandbox forked from

  • https://codesandbox.io/p/github/marmelab/react-admin-sandbox/main (v5)
  • https://stackblitz.com/github/marmelab/react-admin/tree/master/examples/simple (v5)
  • https://stackblitz.com/github/marmelab/react-admin/tree/4.x/examples/simple (v4)

djhi avatar Apr 23 '25 07:04 djhi

I tried to reproduce this issue in react-admin v5.6.4 but couldn't replicate the problem. Here's what I tested:

  1. Created a test form with all the mentioned input types:

    • SelectInput with default value
    • DateInput with default value
    • SelectArrayInput with default value
  2. Test setup:

const PostCreateTest = () => {
  const notify = useNotify();

  const onSubmit = (data) => {
    console.log('Form submission data:', data);
    notify('Form submitted with data: ' + JSON.stringify(data));
  };

  return (
    <Create>
      <SimpleForm onSubmit={onSubmit}>
        <TextInput source="title" required />
        <SelectInput
          source="category"
          choices={[
            { id: 'tech', name: 'Technology' },
            { id: 'lifestyle', name: 'Lifestyle' },
          ]}
          defaultValue="tech"
        />
        <DateInput
          source="publishedAt"
          defaultValue={new Date().toISOString().split('T')[0]}
        />
        <SelectArrayInput
          source="tags"
          choices={[
            { id: 'js', name: 'JavaScript' },
            { id: 'react', name: 'React' },
            { id: 'ts', name: 'TypeScript' },
          ]}
          defaultValue={['js']}
        />
      </SimpleForm>
    </Create>
  );
};
  1. Results:
  • When submitting the form without touching any inputs except the required title field
  • All default values are properly included in the submission data:
{
  "category": "tech",
  "publishedAt": "2025-04-25",
  "tags": ["js"],
  "title": "title"
}

This suggests the issue might be specific to v6.5.4. Could you provide:

  1. A minimal reproduction in v6.5.4 showing the missing values?
  2. The exact steps to reproduce in that version?

You can fork and modify this example: https://stackblitz.com/edit/github-vnan7rfb?file=src%2Fposts%2FPostCreate.tsx,src%2Fposts%2FPostCreateTest.tsx,src%2Fposts%2Findex.tsx

Image

saloua-ch avatar Apr 25 '25 16:04 saloua-ch

@saloua-ch There is no version 6.5.4. We still need a way to reproduce

djhi avatar May 14 '25 08:05 djhi

I can reproduce from the example provided by @saloua-ch. I've come to this issue by using <NumberInput/>. I expected the null default value would be submitted, but it's not.

Image

jmferreiratech avatar Aug 14 '25 16:08 jmferreiratech

@jmferreiratech Thanks for the additional info. However what you have found seems to be an unrelated issue, to me. The inability to use null as default value at the input level seems like a limitation that is not specific to NumberInput, it also happens with TextInput for example. (Btw it can be worked around by passing the default values at the form level). But this does not seem to fit the original issue description IMHO. Since no one has been able to provide a reproduction with the latest version of RA, I'll consider this issue closed. Feel free to reopen if you have a repro (or file a new issue if you want to discuss the NumberInput issue further).

slax57 avatar Aug 18 '25 13:08 slax57