Adding a field to select multiple strings from a fixed list in Sanity.io schema.

2 replies
Last updated: Oct 4, 2023
Hey πŸ‘‹
I'm trying to add a field to my schema so that I can select multiple strings from a list in the UI, for example, the user can select "A", "C", "E" from a list of strings ("A", "B", "C", D", "E"... etc)

Is there a best way to do this. The list to choose from will be fixed and won't need to be coded.

I was looking a array of type string, but I can't find a way to do it
AI Update

You're on the right track! For selecting multiple strings from a fixed list in Sanity, you'll want to use an array of strings with the list option. Here's how to set it up:

{
  name: 'selectedOptions',
  title: 'Select Options',
  type: 'array',
  of: [{
    type: 'string'
  }],
  options: {
    list: [
      {title: 'Option A', value: 'A'},
      {title: 'Option B', value: 'B'},
      {title: 'Option C', value: 'C'},
      {title: 'Option D', value: 'D'},
      {title: 'Option E', value: 'E'}
    ]
  }
}

This gives you a nice multi-select interface in the Studio where users can pick multiple values from your predefined list. The data will be stored as an array like ["A", "C", "E"].

Key points:

  • The type: 'array' with of: [{type: 'string'}] creates an array that holds strings
  • The options.list property on the array field (not the string type inside it) provides the predefined values
  • You can use simple string values in the list (['A', 'B', 'C']) or objects with title and value properties for better labels

If you want the list to appear as checkboxes instead of a dropdown, you can add layout: 'tags' or layout: 'grid' to the options:

options: {
  list: [...],
  layout: 'tags' // or 'grid'
}

This is covered in the Array type documentation under the "predefined strings" example. The array field's copy/paste functionality also works great with this setup if you need to duplicate selections across documents!

If I understand your meaning, the following might be helpful: https://www.sanity.io/docs/array-type#list-dd1300e422eb
Basically you can change the options object in the scheme for the array to render as a list of checkboxes.
Ah that works. Perfect πŸ‘Œ

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?