What you were expecting:
ReferenceInput in a SimpleForm with a default value being pre-populated. It's demonstrated to be working with ra 2.1.0 here https://codesandbox.io/s/2393m2k5rj and described here: https://marmelab.com/blog/2018/07/09/react-admin-tutorials-form-for-related-records.html
What happened instead:
The ReferenceInput stays empty.
Steps to reproduce:
The Post field (which is a ReferenceInput) stays empty.
Related code:
https://codesandbox.io/s/m7w28n9v58
Other information:
n/a
Environment
I have the same issue. After hours of debugging, I can see now what's the cause of this issue.
What it's written in the example code is trying to feed the query parameters in the record object using the defaultValue attribute. meanwhile, there is a code recently added to implement that internally (see that below). https://github.com/marmelab/react-admin/blob/dc3935ec9318c39d0cbb6d26880e57fad8172e5f/packages/ra-core/src/controller/CreateController.tsx#L105
This piece of code does the same of what's done in the example but without parsing (to integer) which would result in duplication of keys like ["1", 1] and will cause that behavior you're facing there.
You may see this warning in the console because of that since both of the keys "1" and 1 will be stringified as "1".
Warning: Encountered two children with the same key, `.$.$1`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
I think there should be a way to disable or override that.
One more note, this can be observed on react-admin#2.8.2 too.
I'm not sure, but the issue should be there since v2.2.0 when <CloneButton /> was introduced because it's mentioned in the docs that <CloneButton /> uses that mechanism.
@christiaanwesterbeek as a workaround, you can pass a fake location to solve the issue like this:
const location = { state: { record: { /** defaults to be entered here **/ } } }
return <Create title="bla bla" {...props} location={location}>
{ /** ... **/ }
</Create>
make sure you pass the location after the {...props} expression, otherwise, your fake location would be overridden by the real one in props.
see this too, it should solve the issue in a clean way:
https://marmelab.com/react-admin/CreateEdit.html#prefilling-a-create-record
Since it turned out that it's not a bug, and moreover everything was mentioned in the link above. I think we just need to update the example codesandbox to reflect the new "prefill" mechanism.
Thanks for your findings @MhdSyrwan
That said, if something worked on the 2.1 and doesn't work on 2.9, it might be a bug (?)
@Kmaschta You're welcome :smile:
Yup, it may be a bug. I think changing the priorities between form defaults and controller defaults would solve the issue and make the old example code work. I mean to make the form defaults override anything else.
The documentation explains how to initialize a create form using the Link state: https://marmelab.com/react-admin/CreateEdit.html#prefilling-a-create-record
We can close this bug.
Most helpful comment
@christiaanwesterbeek as a workaround, you can pass a fake location to solve the issue like this:
make sure you pass the location after the
{...props}expression, otherwise, your fakelocationwould be overridden by the real one inprops.