Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Validation Rule - Only allow one member in an array.

1 replies
Last updated: Feb 26, 2021
I have a validation rule set to only allow 1 member in an array.But, the “Add” button still shows in the studio...
Would be nice if this was conditional so it would be hidden (or dimmed) when the validation rule takes effect.


{   title:"Merke", 
    name:"manufacturer", 
    type: "array", of: [
         {type:'reference',
          to: [ {type:"manufacturer"}]
         }
     ],
     validation: Rule => Rule.max(1).error('Du kan bare ha ett Merke per kjøretøy. Velg Chassisprodusent ved tvil.'),   
    fieldset:"main"
},

Feb 26, 2021, 1:24 PM
Hi Johannes, there's no built-in way to do this for now. However, you could consider customising your array functions. Here's an example that hides the Add button when a max of (1) item is reached, based on the native array component.
To implement it, in your 
sanity.json
 file, add:
{
      "implements": "part:@sanity/form-builder/input/array/functions",
      "path": "./components/CustomArrayFunctions.js"
}
This is the relevant component -  
https://gist.github.com/Grsmto/cc4db257d05898ca60a9572511fa9bcf  (community contribution).
Latest version of the native component for comparison: 
https://github.com/sanity-io/sanity/blob/next/packages/@sanity/form-builder/src/inputs/ArrayInput/ArrayFunctions.tsx
Feb 26, 2021, 4:27 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?