payload icon indicating copy to clipboard operation
payload copied to clipboard

Relationship filterOptions does not update accordingly.

Open HarleySalas opened this issue 1 year ago • 2 comments

In the following reproduction, I have a collection of Movie, Theatre and Upload.

An Upload belongs to a Theatre. A Movie is owned by a Theatre.

An Upload should only be assignable to a Movie, where the Movie's movie.theatre id is equal to the upload.theatre id.

As it stands, the following code works out for this logic:

  {
          name: 'theatre',
          type: 'relationship',
          relationTo: COLLECTION_SLUG_THEATRE,
          hasMany: false,
          admin: {
            allowCreate: false,
            width: '50%',
          },
        },
        {
          name: 'upload',
          type: 'relationship',
          relationTo: COLLECTION_SLUG_UPLOAD,
          admin: {
            allowCreate: false,
            width: '50%',
          },
          unique: true,
          filterOptions: ({ data }) => {
            if (data.theatre) {
              return {
                theatre: {
                  equals: data.theatre,
                },
              }
            } else return false
          },
        },

However, If you have TheatreA selected, and select UploadA, then change video.theatre to TheatreB, UploadA is not removed. The user experience is seemingly impossible to make reasonable, with making entirely custom field components. You can play around with it and see numerous scenarios where the filterOptions is not fixed until refreshing the page, as well. For example, it's possible to have both UploadA and UploadB selectable in the select component, which should certainly never happen.

I believe that in this scenario, it should not only ensure that the filterOptions are always correct, but it should remove a selected item, if it's not present in the filterOptions.

If you'd like to clone the reproduction, to easily play around with this, you can follow these instructions:

  1. Run git clone -b filter-options-wont-update https://github.com/HarleySalas/payload-3-reproductions.git
  2. Run pnpm install
  3. Change .env.example to .env
  4. Run docker-compose up
  5. Run pnpm dev
  6. Go to http://localhost:3000/admin/collections/movie and select a Movie

There is no need to modify anything inside of .env. All necessary data will be seeded.

HarleySalas avatar May 15 '24 23:05 HarleySalas

Here are two recordings to try and illustrate the issue. Take note of how after certain actions, the option to select something which should be selectable disappears. There's a few funky things going on there, but I think the picture can provide more clarity than words.

https://github.com/payloadcms/payload-3.0-demo/assets/38236585/fac6a32f-1ad5-4924-8812-c563f63f6b40

https://github.com/payloadcms/payload-3.0-demo/assets/38236585/b94af0c8-f32b-437a-acdd-c8e26be54541

HarleySalas avatar May 16 '24 08:05 HarleySalas

This issue has been marked as stale due to lack of activity.

To keep this issue open, please indicate that it is still relevant in a comment below.

github-actions[bot] avatar Dec 13 '24 05:12 github-actions[bot]

This issue was automatically closed due to lack of activity.

github-actions[bot] avatar Dec 21 '24 05:12 github-actions[bot]

Also facing this issue, +1

Sandro48 avatar Jan 26 '25 09:01 Sandro48

You can handle this with a custom component on the collection. Without rewriting the relationship field you could use a type UI field that just uses form hooks to watch for the change and sets the value of uploads if your condition is met.

There are loads of scenarios where validation logic changes and you get an error only when trying to save. I don't consider this an issue so I will convert to a discussion.

DanRibbens avatar Apr 22 '25 19:04 DanRibbens