Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

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 -  (community contribution).
Latest version of the native component for comparison:

Feb 26, 2021, 4:27 PM Get the most out of your content is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.