#Render a field conditionally, based on a value from relationship field's document

10 messages · Page 1 of 1 (latest)

verbal rover
#

Is there a way to get productUnit.isContainer without having to use API eg: payload.findByID? Is using the API here even efficient considering the data I need was already returned on page load and/or when user selects relationship field

Simplified example:

export const ProductUnits: CollectionConfig = {
  slug: 'product-units',
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
    },
    {
      name: 'isContainer',
      type: 'checkbox',
      defaultValue: false,
    },
  ]
}

export const Products: CollectionConfig = {
  slug: 'products',
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
    },
    {
      name: 'productUnit',
      type: 'relationship', 
      relationTo: 'product-units',
      required: true,
    },
    {
      name: 'allowManifest',
      type: 'checkbox',
      admin: {
        condition: (data, siblingData, { user }) => {
          // How to get isContainer?
          if(data.productUnit.isContainer){ 
            return true
          }
          else{
            return false
          }
        }
      },
    }
  ]
}```
twilit dawn
#

Hmm I think you'll need to run a findById for this to work as you've mentioned. The productUnit you get is just an ID value right?

verbal rover
#

Thanks! Seems a bit inefficient. That’s Correct. I get the ID of the chosen product unit.

twilit dawn
#

I don't think it's much overhead at all to be honest, should be fine

#

But I know what you mean, it'd be nice if there's some depth property to pass into it where it just populates with the full doc instead

#

That way no need for an extra api call

verbal rover
#

Agreed. I'm not using payload.findById() inside of condition is the right way to go about it as it complains because condition is not expecting a promise. I think I may need to create a custom field.

twilit dawn
#

Ahhh right, it's just expecting a boolean... yeah that sounds like custom field territory then

lost shard
#

I constantly run into this issue myself. It would be create if we can maybe refactor the condition to accept a promise, or set an “admin depth” for field depth we want resolved for relationship in admin panel