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.

Condition inside Array type field

default discord avatar
eustachi0last year
2

I have an array type with some fields, for each array of fields I have a value that depends on another value, a condition. These values are accessible by the index of the array. So for example

if (data?.array?.[0].valueX === Y) return true

but then when I add more arrays how can I control the condition to all the items in the array. I tried with a for loop, array.some, but it doesn't work.



Here

endDate

depends on the value of a field called

duration
        ...
        name: "endDate",
        type: "date",
        admin: {
            condition: (data) => {
                let result = false;
                for (let i = 0; i < data?.array?.length; i++) {
                    if (data?.array?.[i]?.duration === "limitedTime") {
                        result = true;
                    }
                }
                return result;
            },
        }
        ...


This has worked for me (easy):


        admin: {
            condition: (data, siblingData) => {
                return siblingData?.duration === "limitedTime";
            },
        },
  • default discord avatar
    brianjmlast year

    I found this seeking an answer to a similar question. In my case, I'm trying to hide a checkbox field in an array where only one row checkbox can be checked/shown. For example:



    I have an array of phone numbers. All have a "primary" checkbox but only one can be checked (in the entire array). If one is checked, I want to hide all "primary" checkboxes that are not checked. If I have 4 phone numbers, only one can be the primary.



    Any ideas? All I can come up so far is using a hook but I'd like to control the display.



    I resolved my question with the following

    condition

    :



    // src/fields/checkbox/index.ts
    
    import { CheckboxField } from 'payload/types'
    
    export const primaryCheckboxField: CheckboxField = {
      name: 'primary', // required
      type: 'checkbox', // required
      label: 'Primary',
      defaultValue: false,
      admin: {
        condition: (data, siblingData, { user }) => {
          const phones = data?.phones || [],
                row = {
                  id: siblingData?.id,
                  primary: siblingData?.primary,
                }
          let show = true
          
          // Current row; primary is checked
          if (row.primary) return true
    
          // Another array item is checked
          phones.some(item => {
            if (
              row.id !== item.id &&   // Not current row
              item.primary            // Primary is checked
            ) {
              show = false
              return true
            }
          })
    
          return show
        },
      }
    }
  • default discord avatar
    eustachi0last year

    Hey @BrianJM I've just seen this. Great that you solved your particular use case. Thanks for sharing the answer.

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..