Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Using User object in Conditional Logic

default discord avatar
kaspartr2 years 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.krmr2 years 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

    @969226489549713438

    added this from discussion

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

    with PR

    https://github.com/payloadcms/payload/pull/2543
  • default discord avatar
    tinouti2 years ago

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

  • discord user avatar
    dribbens
    2 years 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.krmr2 years ago
    @233209174069084161

    @969226489549713438

    thanks 🙏 and sorry, it was my mistake 🙈


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

  • default discord avatar
    tinouti2 years ago

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

  • default discord avatar
    andyteecflast year
    @946299884070375424
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.