Hi!
I am building this admin where in one collection, I have an address field which relates to another collection. How do I restrict the admin UI to stop listing all the available addresses and only show the selected one, if not selected any then it should only show the '+' button
What is happening right now: [Attached]
Collections:
Vendors.ts:
||import { CollectionConfig } from "payload/types";
const Vendors: CollectionConfig = {
slug: "vendors",
admin: {
useAsTitle: "vendorName",
},
fields: [
{
name: "vendorName",
type: "text",
required: true,
},
{
name: "vendorCode",
type: "text",
admin: {
readOnly: true,
},
required: false,
},
{
name: "contactPerson",
type: "relationship",
relationTo: "contact-person",
required: true,
hasMany: false,
admin: {
disableListColumn: true,
},
},
{
name: "bankAccount",
type: "relationship",
relationTo: "bank-accounts",
required: true,
hasMany: false,
admin: {
disableListColumn: true,
},
},
{
name: "address",
type: "relationship",
relationTo: "addresses",
required: true,
hasMany: false,
filterOptions: ({ relationTo, siblingData }) => {
if (relationTo === "addresses") {
return {
and: [],
};
}
},
admin: {
disableListColumn: true,
disableListFilter: true,
isSortable: false,
},
},
],
};
export default Vendors;
||Found the answer. Anybody needs help on this can ping me
Hey i think i have a similar issue,
I have a categories collection and a product collection
Each product has a relationship with a category
And a category has relationships with the products
How do i make it so when choosing the relationship in the category collection that only products with that category relationship show up?
I made a similar design with categories and sub categories so that only sub categories from that category show up in the dropdown. Here is the code:
import { CollectionConfig } from "payload/types";
const Categories: CollectionConfig = {
slug: "categories",
admin: {
useAsTitle: "name",
description: "Categories of products",
},
fields: [
{
name: "name",
type: "text",
required: true,
},
{
name: "description",
type: "text",
required: false,
},
{
name: "image",
type: "upload",
relationTo: "media",
},
{
name: "subCategory",
type: "relationship",
relationTo: "sub-categories",
required: true,
hasMany: true,
filterOptions: ({ relationTo, data, siblingData }) => {
console.log(relationTo); // Type of realtion (The slug for the child collection)
console.log(data); // The actual collection
console.log(siblingData); // Whatever being edited on screen
if (relationTo === "sub-categories") {
if (siblingData["subCategory"]) { // This applies to when you are editing the collection
return {
_id: {
in: siblingData["subCategory"], // This is the key. It follows mongo like query syntax. You can read more here: https://payloadcms.com/docs/queries/overview
},
};
return true;
} else { // This applies to when you are creating the collection for the first time.
return false;
}
}
},
},
],
};
export default Categories;any idea about this ts error ?
Don't use '.' operator. use siblingData['products']
still get the same error
Did you fix this? Can you paste the complete code?
Sadly no i still get the same error
Share the code, I'll take a look.
I dont have my laptop rn, but its the exact same one u send me but i just modified the collection
Ok. You can share the code once you get some time. I spent hours on this trying to resolve.
import { CollectionConfig } from 'payload'
const Categories: CollectionConfig = {
slug: 'categories',
admin: {
useAsTitle: 'name',
group:'Ecommerce'
},
access: {
read: () => true,
},
fields: [
{
name: 'image',
type: 'upload',
label: 'Category Image',
relationTo:'media',
required: true,
},
{
name: 'name',
type: 'text',
label: 'Name',
required: true,
localized: true,
},
{
name: 'description',
type: 'text',
label: 'Description',
localized: true,
},
{
name: 'products',
type: 'relationship',
relationTo: ['products','splitPizza'],
hasMany: true,
filterOptions: ({ relationTo, data, siblingData }) => {
console.log(relationTo); // Type of realtion (The slug for the child collection)
console.log(data); // The actual collection
console.log(siblingData); // Whatever being edited on screen
if (relationTo === "products") {
if (siblingData["products"]) { // This applies to when you are editing the collection
return {
_id: {
in: siblingData["products"], // This is the key. It follows mongo like query syntax. You can read more here: https://payloadcms.com/docs/queries/overview
},
};
return true;
} else { // This applies to when you are creating the collection for the first time.
return false;
}
}
return false
},
],
}
export default Categorieshere is the colleciton code
yeah ?
so its 'normal' that typescript doesn t like it
you can cast it to whatever type you need
so i just let it be ?
i think so
but it still dosent work
Does it run or shows error in console?
yeah
Code seems ok to me
Isn't it just a TypeScript error? Normally, the code should be able to execute
Star
Discord
online
Get dedicated engineering support directly from the Payload team.