Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

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", 
    type: "array", of: [
          to: [ {type:"manufacturer"}]
     validation: Rule => Rule.max(1).error('Du kan bare ha ett Merke per kjøretøy. Velg Chassisprodusent ved tvil.'),   

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 
 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: 
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?