Skip to content
Sveltia CMS
Esc
↑↓navigate↵open⌘Jpreview
On this page

Select Field

Choose single or multiple options in Sveltia CMS from predefined lists.

The Select field type allows users to choose one or more options from a predefined list within the CMS entry form.

User Interface

Editor

Radio buttons (single select) or checkboxes (multi select) for choosing options. If there are many entries, a dropdown with search functionality will be used instead. Use the dropdown_threshold option to customize when to switch to the dropdown UI.

For multi-select options with many entries, a tag input UI will be used instead of checkboxes to save space. Items can be reordered by dragging and dropping or using the right/left arrow keys. Items can also be removed by clicking the ✕ icon on each item.

Preview

A string or a list of strings representing the selected option(s).

Data Type

It depends on the options. Usually a string or an array of strings, depending on whether the multiple option is set to true or false, but can also be a number, boolean or null, or an array of them, if the options are defined as such.

If the required option is set to false and no option is selected, the value will be an empty array for multi select. For single select, it will be an empty string if the options are strings, or null if they are numbers or booleans.

Data Validation

  • If the required option is set to true, at least one option must be selected. An option with the value null or an empty string counts as a selection.
  • If the multiple option is enabled, the number of selected options must be between the min and max limits, if specified.
  • If the pattern option is provided, the selected option value must match the regular expression. Numbers are tested as strings, and the pattern is not tested while nothing is selected.
    • For multi select, the selected values joined with commas, e.g. foo,bar,baz, must match instead. As with Decap CMS, the pattern is tested against the whole selection, not against each value. To accept lowercase values only, use ^[a-z]+(,[a-z]+)*$ rather than ^[a-z]+$.

Options

In addition to the common field options, the Select field supports the following options:

Required Options

widget

  • Type: string
  • Default: string

Must be set to select.

options

  • Type: array

An array of options for the select field. Each option can be defined as a string, number, boolean or null, or as an object with label and value properties. These options will be presented to the user in the UI. An empty list or a list with duplicate values is reported as a config validation error on the login screen.

TOML doesn’t support null, so an option with the value null can’t be defined in a TOML configuration file. In a TOML data file, a field with the value null is omitted.

The following are valid examples of the options configuration:

options:
  - London
  - Paris
  - New York
options = ["London", "Paris", "New York"]
{
  "options": ["London", "Paris", "New York"]
}
options: ['London', 'Paris', 'New York'],
options: [1, 2, 3]
options = [1, 2, 3]
{
  "options": [1, 2, 3]
}
options: [1, 2, 3],
options:
  - { label: Toronto, value: YYZ }
  - { label: Vancouver, value: YVR }
  - { label: Montreal, value: YUL }
[[options]]
label = "Toronto"
value = "YYZ"
[[options]]
label = "Vancouver"
value = "YVR"
[[options]]
label = "Montreal"
value = "YUL"
{
  "options": [
    { "label": "Toronto", "value": "YYZ" },
    { "label": "Vancouver", "value": "YVR" },
    { "label": "Montreal", "value": "YUL" }
  ]
}
options: [
  { label: 'Toronto', value: 'YYZ' },
  { label: 'Vancouver', value: 'YVR' },
  { label: 'Montreal', value: 'YUL' },
],
options:
  - { label: Red, value: 1 }
  - { label: Green, value: 2 }
  - { label: Blue, value: 3 }
[[options]]
label = "Red"
value = 1
[[options]]
label = "Green"
value = 2
[[options]]
label = "Blue"
value = 3
{
  "options": [
    { "label": "Red", "value": 1 },
    { "label": "Green", "value": 2 },
    { "label": "Blue", "value": 3 }
  ]
}
options: [
  { label: 'Red', value: 1 },
  { label: 'Green', value: 2 },
  { label: 'Blue', value: 3 },
],

Optional Options

default

  • Type: string, number, boolean, null, an object with label and value properties, or an array of them
  • Default: an empty string, null or []

The default value for the field. Should be a string, number, boolean or null for single select, or an array of them for multi select, depending on the multiple option. An option can also be copied as is from the options list, as an object with label and value properties. In that case, only its value is saved.

The following are reported as a config validation error on the login screen:

  • a value that isn’t one of the options
  • an array with multiple off
  • a single value with multiple on
  • Type: integer
  • Default: 5

The number of options at which to switch from radio buttons/checkboxes to a dropdown UI. If the number of options exceeds this threshold, a dropdown with search functionality will be used.

multiple

  • Type: boolean
  • Default: false

Whether to allow selecting multiple options.

min

  • Type: integer
  • Default: 0

The minimum number of options required. This enables validation to ensure that users select at least this many options. Ignored if multiple is set to false.

max

  • Type: integer
  • Default: Infinity

The maximum number of options allowed. This enables validation to prevent users from selecting more than this many options. Ignored if multiple is set to false.

Examples

Single Select

The following example shows a basic single select field for choosing a country.

- name: country
  label: Country
  widget: select
  options:
    - USA
    - Canada
    - Mexico
[[fields]]
name = "country"
label = "Country"
widget = "select"
options = ["USA", "Canada", "Mexico"]
{
  "fields": [
    {
      "name": "country",
      "label": "Country",
      "widget": "select",
      "options": ["USA", "Canada", "Mexico"]
    }
  ]
}
{
  fields: [
    {
      name: 'country',
      label: 'Country',
      widget: 'select',
      options: ['USA', 'Canada', 'Mexico'],
    },
  ],
}

Output example when “Canada” is selected:

country: Canada
country = "Canada"
{
  "country": "Canada"
}

Custom Labels and Values

The options can be defined with custom labels and values. The following example shows a select field for choosing a color with specific hex values.

- name: color
  label: Color
  widget: select
  options:
    - { label: Red, value: '#FF0000' }
    - { label: Green, value: '#00FF00' }
    - { label: Blue, value: '#0000FF' }
[[fields]]
name = "color"
label = "Color"
widget = "select"
[[options]]
label = "Red"
value = "#FF0000"
[[options]]
label = "Green"
value = "#00FF00"
[[options]]
label = "Blue"
value = "#0000FF"
{
  "fields": [
    {
      "name": "color",
      "label": "Color",
      "widget": "select",
      "options": [
        { "label": "Red", "value": "#FF0000" },
        { "label": "Green", "value": "#00FF00" },
        { "label": "Blue", "value": "#0000FF" }
      ]
    }
  ]
}
{
  fields: [
    {
      name: 'color',
      label: 'Color',
      widget: 'select',
      options: [
        { label: 'Red', value: '#FF0000' },
        { label: 'Green', value: '#00FF00' },
        { label: 'Blue', value: '#0000FF' },
      ],
    },
  ],
}

Output example when “Green” is selected:

color: '#00FF00'
color = "#00FF00"
{
  "color": "#00FF00"
}

Boolean and Null Values

Option values can be booleans or null, which is useful when a choice needs custom labels or a third state. The following example shows a required field with “Yes”, “No” and “Not relevant” options, stored as true, false and null. Since null is one of the options, it counts as a selection and passes the required validation.

TOML doesn’t support null, so this example is only available in YAML, JSON and JavaScript.

- name: accessible
  label: Wheelchair Accessible
  widget: select
  options:
    - { label: 'Yes', value: true }
    - { label: 'No', value: false }
    - { label: Not relevant, value: null }
  default: null
{
  "fields": [
    {
      "name": "accessible",
      "label": "Wheelchair Accessible",
      "widget": "select",
      "options": [
        { "label": "Yes", "value": true },
        { "label": "No", "value": false },
        { "label": "Not relevant", "value": null }
      ],
      "default": null
    }
  ]
}
{
  fields: [
    {
      name: 'accessible',
      label: 'Wheelchair Accessible',
      widget: 'select',
      options: [
        { label: 'Yes', value: true },
        { label: 'No', value: false },
        { label: 'Not relevant', value: null },
      ],
      default: null,
    },
  ],
}

Output example when “No” is selected:

accessible: false
accessible = false
{
  "accessible": false
}

Multi Select with Limits

The following example shows a multi select field for choosing fruits, with a minimum of 1 and a maximum of 3 selections.

- name: fruits
  label: Fruits
  widget: select
  options:
    - Apple
    - Banana
    - Cherry
    - Date
  multiple: true
  min: 1
  max: 3
[[fields]]
name = "fruits"
label = "Fruits"
widget = "select"
options = ["Apple", "Banana", "Cherry", "Date"]
multiple = true
min = 1
max = 3
{
  "fields": [
    {
      "name": "fruits",
      "label": "Fruits",
      "widget": "select",
      "options": ["Apple", "Banana", "Cherry", "Date"],
      "multiple": true,
      "min": 1,
      "max": 3
    }
  ]
}
{
  fields: [
    {
      name: 'fruits',
      label: 'Fruits',
      widget: 'select',
      options: ['Apple', 'Banana', 'Cherry', 'Date'],
      multiple: true,
      min: 1,
      max: 3,
    },
  ],
}

Output example when “Apple” and “Cherry” are selected:

fruits:
  - Apple
  - Cherry
fruits = ["Apple", "Cherry"]
{
  "fruits": ["Apple", "Cherry"]
}

Was this page helpful?