Using User object in Conditional Logic

default discord avatar
kaspartr
7 months ago
6

According to the documentation here

https://payloadcms.com/docs/fields/overview#conditional-logic

one should be able to access the User object in the condition - show/hide field based on user data.



When I copy the example into my code, I get the following error.


 
Type '(data: any, siblingData: any, { user }: { user: any; }) => boolean' is not assignable to type 'Condition<any, any>'


Is the documentation actual, or am I doing something wrong. Here is my code (inside tabs, inside row):



{
  name: 'sourceAsset',
  label: 'Offset Account',
  type: 'relationship',
  relationTo: 'assets',
  admin: {
    description: 'Some description',
    condition: (data, siblingData, { user }) => { //<--- ERROR
      return true;
    }
  },
  access: {
    create: isAssetManagerFieldLevel,
    update: isAssetManagerFieldLevel,
    read: isLoggedInFieldLevel,
  },
  filterOptions: ({ relationTo, data }) => {
    return {...}
  }
}


When I escape this error with ts-ignore, then I get the following runtime error:



caught (in promise) TypeError: Cannot read properties of undefined (reading 'user')
  • default discord avatar
    dnl.krmr
    6 months ago

    I'm currently also struggling with this



    Within the condition type user is also not defined


    export type Condition<T extends TypeWithID = any, P = any> = (data: Partial<T>, siblingData: Partial<P>) => boolean;

    But it is included within docs and @dribbens added this from discussion

    https://github.com/payloadcms/payload/discussions/2541

    with PR

    https://github.com/payloadcms/payload/pull/2543
  • default discord avatar
    tinouti
    6 months ago

    Just to rule out the obvious, your Payload version is > 1.7.2, right?

  • discord user avatar
    dribbens
    Payload Team
    6 months ago

    The condition change should be in place for all field types. It has to be that you're on an older version of Payload as Tinouti suggested. If you're seeing this on @latest, I would create an issue on github.

  • default discord avatar
    dnl.krmr
    6 months ago

    @tinouti @dribbens thanks 🙏 and sorry, it was my mistake 🙈


    I thought I was sure I had a version after this merge date 🤦‍♂️

  • default discord avatar
    tinouti
    6 months ago

    Haha, glad to hear this was all this was! 🙌

  • default discord avatar
    andyteecf
    4 months ago

    @allannnc

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.