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

ReferenceInput and SelectInput showing "Associated reference no longer appears to be available."

Open trsteel88 opened this issue 8 years ago • 13 comments

What you were expecting: SelectInput to populate with the selected value.

What happened instead: SelectInput is populated with the selected value. However, underneath it shows "Associated reference no longer appears to be available."

My form looks like this:

export const TemplateEdit = props => (
    <Edit {...props}>
        <SimpleForm>
            <TextInput source="name" />

            <ArrayInput label="Shots" source="shots">
                <SimpleFormIterator>
                    <TextInput source="name" />

                    <ReferenceInput
                        source="shotType"
                        reference="shot-types"
                    >
                        <SelectInput optionText="name" optionValue="@id" />
                    </ReferenceInput>
                </SimpleFormIterator>
            </ArrayInput>
        </SimpleForm>
    </Edit>
);

The API returns the value "/shot-types/1" for "shotType". The response to populate the dropdown looks like this:

[
        {
            "@id": "/shot-types/1",
            "@type": "ShotType",
            "id": 1,
            "name": "Shot Type 1"
        },
        {
            "@id": "/shot-types/2",
            "@type": "ShotType",
            "id": 2,
            "name": "Shot Type 2"
        }
]

Here is a screenshot of what occurs:

screen shot 2018-11-28 at 5 14 38 pm

It's strange that the value shows as selected but the warning appears saying it no longer exists.

trsteel88 avatar Nov 28 '18 07:11 trsteel88

Fyi, select input saves correctly and everything works. It just shows that warning saying it isn't available (but it matches it correctly).

trsteel88 avatar Nov 28 '18 07:11 trsteel88

I've never used a ReferenceInput inside an ArrayInput, so it's not tested. Would you mind setting up a CodeSandbox to show the issue?

fzaninotto avatar Dec 05 '18 18:12 fzaninotto

This occurs outside an ArrayInput as well.

I found a stackoverflow where someone else is trying to use a different optionvalue as well: https://stackoverflow.com/questions/51021661/react-admin-using-field-other-than-id-for-referenceinput

It's like optionValue doesn't work properly on the ReferenceInput child.

trsteel88 avatar Dec 06 '18 00:12 trsteel88

@fzaninotto I could resolve the issue by converting the value before ReferenceInput gets it. Is there a way I could do this?

Basically I just need to convert /shot-types/2 to 2 and it will resolve everything.

trsteel88 avatar Dec 10 '18 23:12 trsteel88

I just used the parse and format property on ReferenceInput. There is still a problem with optionValue not working properly though.

Code below if anyone else is looking for a quick solution

const formatIriAsInt = v => {    
    if (v.indexOf('/') >= 0) {
        v = v.split('/');

        return parseInt(v[v.length - 1]);
    }

    return v;
}

<ReferenceInput
    source="shotTypes"
    reference="shot-types"
    parse={v => v ? '/organisations/' + v : v}
    format={formatIriAsInt}
    {...rest}>
    <SelectInput optionText="name" />
</ReferenceInput>

trsteel88 avatar Dec 11 '18 00:12 trsteel88

Again, please provide a minimal CodeSandbox to let us reproduce the issue.

fzaninotto avatar Feb 07 '19 11:02 fzaninotto

@trsteel88 just out of curiosity - if you render many references ( based on ReferenceInput ) does each one call your API?

matigda avatar Feb 13 '19 15:02 matigda

@matigda No, in that case, we accumulate the individual GET_ONE calls into a single GET_MANY

djhi avatar Feb 13 '19 15:02 djhi

@djhi hi, thank you very much for that quick response. I know it says so in the docs, but this piece of code:

export const SliderEdit = props => (
    <Edit {...props}>
        <SimpleForm>
            <TextField source="id" label={'Id'}/>
            <LongTextInput source="name" />
            <NumberInput source="status" />
            <ArrayInput source="bannerSliders">
                <SimpleFormIterator>
                    <ReferenceInput label="Banner" source="banner.id" reference="banners">
                        <SelectInput optionText="name" />
                    </ReferenceInput>

                    <NumberInput source="position" />
                </SimpleFormIterator>
            </ArrayInput>
        </SimpleForm>
    </Edit>
);

makes a call for each ReferenceInput. So that's why I asked author. Sorry for messing up the issue. I've already created the issue on StackOverflow: https://stackoverflow.com/questions/54632761/is-there-any-cache-in-react-admin/54642028

matigda avatar Feb 13 '19 15:02 matigda

I was experiencing the same issue as @trsteel88; I was trying to reference a different Resource using a different value to "id" and passing an optionValue.

The ReferenceInput all worked fine. I needed a quick fix in an edit page, so hid the FormHelperTextProps on the field (see below). Obviously this is hacky, and wouldn't typically advocate .

#edit.js:
<SelectInput
  FormHelperTextProps={{ classes: { root: 'hide-helper' } }}
  optionText="name"
  optionValue="iconKey"
/>
#index.css:
.hide-helper {
  display: none;
}```

atomicacorn avatar May 02 '19 16:05 atomicacorn

Since that level of overlapping doesn't seem to be a common need, and that no CodeSandbox were provide in order to help us reproduce the issue, I'm closing this one.

Feel free to re-open with a CodeSandbox.

Kmaschta avatar Jul 23 '19 17:07 Kmaschta

I'm able to reproduce that problem using CodeSandbox. If you look into console it'll show 3 queries to REST that are absolutely identical.

sliterok avatar Oct 11 '20 03:10 sliterok

<ReferenceInput label="Banner" source="banner.id" reference="banners"> <SelectInput optionText="name" /> </ReferenceInput> when we use this, how can we catch its value, as banner.id or as something else?

fullstacksk avatar Nov 20 '20 06:11 fullstacksk