Weakky/ra-data-opencrud

ReferenceArrayInput works on Edit, but not Create

EarthlingDavey opened this issue · 3 comments

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.

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

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 ?

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.