Dynamic select field values

default discord avatar
samtietjen
3 months ago
10

Here's a mock collection schema:



burgers: {
   fields: {
     name: text
     toppings: array[ingredient<ref>, variety<string>]
   }
}

ingredients: {
   fields: {
     name: text
     varieties: string[]
   }
}


We create a burger with the name "Spicy Cheese Burger".



We add a new topping to the array, select "Cheese" which is a reference to ingredients, and a select field appears with the varieties of cheeses. This let’s us select "Pepper Jack".



Any thoughts on how to accomplish something like this?

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    if you want dynamic select values, there are a few things to consider



    to me, "ingredients" would be a separate collection and then you'd just use a relationship field with a

    filterOptions

    function to narrow down the available options



    but if you didn't want to use a relationship field, then my answer would be different



    a "select" field is an

    enum

    - - which means that the options available need to be

    known

    and can't be dynamic



    if you don't know all the potential options, then you should actually make a text field, and then provide a custom component - wherein, you'd render a select field and just provide the select field with the dynamic options that you need



    the reason is that a text field isn't an enum - - it will accept any text value... whereas a select field, you need to know the options in advance

  • default discord avatar
    samtietjen
    3 months ago

    Oh yeah my bad, should’ve specified this. So those two are different collections. The ref is to “cheese” in ingredients. We don’t want “cheese” in our new burger item though. We want “pepper jack” which is from an array within “cheese”. From what I’m hearing though it seems like the only way to go is to create each variety of cheese as it’s own item in the collection. Rather referencing a value (pepper jack) within the cheese item.

  • default discord avatar
    jarrod69420
    3 months ago

    Is there a reason you group the cheeses into 1 ingredient instead of creating a doc for each cheese? Might be easier to do it that way!

  • default discord avatar
    samtietjen
    3 months ago

    This is an extremelllyyy abstract example because the company I’m working with is in manufacturing, so the terminology can get wild. I think you’re right though. I’ll try and solve it through better architecture instead.



    Thanks guys!

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.