Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceArrayInput works on Edit, but not Create #25

Open
EarthlingDavey opened this issue Jan 16, 2019 · 3 comments
Open

ReferenceArrayInput works on Edit, but not Create #25

EarthlingDavey opened this issue Jan 16, 2019 · 3 comments

Comments

@EarthlingDavey
Copy link

This is demonstrated on the code sandbox.

To reproduce:

  1. Click the Options tab on the left,
  2. Create a new Option
  3. Add one or more Values
  4. Save

Expected behaviour: the Values are saved to the new Option.
Result: the Values are not be saved to the new Option.

  1. Click the Options tab on the left,
  2. Edit an existing Option
  3. Add one or more Values
  4. Save

Result: This works as expected.

With some guidance, I could have a crack at fixing this.

@marcantoine
Copy link

I encounter this problem as well and took a look under the hood.

The Values are passed to the dataProvider in "valuesIds".
It doesn't works on CREATE because there is no fields corresponding to valuesIds in the instrospectionResults.
It works on UPDATE because, beside the "valuesIds" field, react-admin send also previous data in a field "values". This values field is found, and then buildUpdateVariables() process the data from "valuesIds".
It seems to me that the fact it works on UPDATE is a "coincidence".

I understand that the convention for naming source of ReferenceArrayInput is "fieldname + Ids". I propose to rename the key to remove Ids :

On CREATE (buildCreateVariables) or UPDATE (buildUpdateVariables), if we detect that the field contains an array, we should look for the field corresponding to "values" instead "valuesIds". I'd suggest we remove the "Ids" part of the key.
We should make sure the field is contains the suffix "Ids" before removing it, otherwise it will also impact data

@marcantoine
Copy link

Dont know if I can propose a PR, but adding this line in buildCreateVariables and buildUpdateVariables fix the problem

 if (Array.isArray(params.data[key])) {
        key = key.slice(-3) === "Ids" ? key.slice(0, -3) : key

@Weakky Could you please take a look or guide us in order to fix this ?

@5achinJani
Copy link

5achinJani commented Feb 25, 2020

I got this working by removing the Ids from the code.
Instead of creating a fork and making this change I'd recommend use the following fork which has already handled this fix(in a better way maybe) and has some other fixes too :

Hey @marcantoine thanks for this fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants