Skip to content

Number Field ​

The Number field type allows users to input numeric values using a specialized input field that supports incrementing and decrementing values.

User Interface ​

Editor ​

A text input field that only accepts numeric values. It includes up and down arrows for incrementing or decrementing the value, as well as support for decimal points and negative numbers.

Additional text can be displayed before or after the input field using the before_input and after_input options.

A field with the auto_increment option is read-only, as its value is assigned automatically. The field is hidden while a new entry is being created.

Preview ​

A localized string representation of the number, formatted according to the preview locale. All the decimal places of the value are shown, without rounding.

Data Type ​

A number if the value_type option is set to int (default) or float. If the required option is set to false and the field is left empty, the value will be null.

If value_type is other than int or float, the value will be stored as a string.

Data Validation ​

  • If the required option is set to true, the number value must not be null (i.e. the field must not be left empty).
  • The number value must be within the range defined by the min and max options, if specified.
  • The number value must conform to the type defined by the value_type option.
  • The input must be a number the field can store. Text that isn’t a number, such as abc, an infinite number and, with value_type: int, an integer beyond the range JavaScript can represent exactly (253) are reported as invalid rather than being saved as an empty value.
  • If the unique option is set to true, the number must not be used in the same field of any other entry in the collection.

A field with the auto_increment option is not validated, as its value is assigned automatically.

Options ​

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

Required Options ​

widget ​

  • Type: string
  • Default: string

Must be set to number.

Optional Options ​

Breaking change from Netlify/Decap CMS

Sveltia CMS does not support the deprecated camelCase valueType option. Use value_type instead.

default ​

  • Type: number or string
  • Default: null

The default value for the field. It has to be a number of the value_type: an integer for int and int/string, any number for float and float/string. A string is parsed as a number. A value that can’t be parsed is reported as a config validation error on the login screen, because the field would otherwise come up empty.

value_type ​

  • Type: string
  • Default: int

The type of value to store. Can be one of the following:

  • int: The UI only accepts integer input, and the value is stored as an integer.
  • float: The UI accepts decimal input, and the value is stored as a floating-point number.
  • int/string: The UI only accepts integer input, but the value is stored as a string.
  • float/string: The UI accepts decimal input, but the value is stored as a string.

Note for Netlify/Decap CMS users

The Netlify/Decap CMS document says the value_type option accepts any type other than int and float, which results in the value being stored as a string. However, this doesn’t actually work in Decap CMS. So, Sveltia CMS officially supports only int and float, along with the new int/string and float/string types. For backward compatibility, other types are treated like int/string: the UI only accepts integer input, and the value is stored as a string.

min ​

  • Type: number
  • Default: -Infinity

The minimum allowed value for the field. This enables validation to ensure that users enter a value greater than or equal to this minimum.

Setting it to 0 or higher also brings up a numeric keypad on mobile devices. Without it, the full keyboard is shown, because the numeric keypad on iOS has no minus key for entering a negative number.

max ​

  • Type: number
  • Default: Infinity

The maximum allowed value for the field. This enables validation to ensure that users enter a value less than or equal to this maximum.

step ​

  • Type: number
  • Default: 1

The increment/decrement step for the input field. This determines the amount by which the value changes when using the up and down arrows.

Set it to -1 to hide the up and down buttons and disable stepping with the arrow keys. This is useful for a number that isn’t meant to be stepped, like an ID copied from another system.

auto_increment ​

  • Type: boolean
  • Default: false

Whether to number the entries automatically when they’re saved, like an AUTO_INCREMENT column in a relational database. This is useful for an invoice number, a podcast episode number or any other sequential ID. See the example below.

  • A new entry is given the integer after the highest number any entry in the collection has, including the unpublished entries with Editorial Workflow. The first entry is numbered 1, or the min value if it’s set, so min: 1000 starts the numbering from 1000, and min: 0 from 0.
  • The number is assigned when the entry is saved, before its slug is created, so it can be used in the collection’s slug template.
  • An existing entry keeps its number. An entry saved before the option was enabled is given one the next time it’s saved.
  • A duplicated entry is given a new number.
  • With a value_type that stores the value as a string, such as int/string, the number is stored as a string.

If the number is only used as the slug and doesn’t need to be stored in the entry, use the collection’s slug.auto_increment option instead.

The option is only available in an entry collection. It’s ignored in a file collection and the Hugo’s special index file, as well as for a field in a List field or a rich text editor component.

WARNING

Unlike a database, Sveltia CMS doesn’t keep a counter: the next number is worked out in the browser from the entries that exist when an entry is saved. This has two consequences:

  • Deleted entries aren’t tracked, so a number can be reused. If the entry with the highest number is deleted, the next new entry is given the same number, even though it’s a different entry. Avoid deleting numbered entries if the numbers must never be reused, e.g. when they’re referenced from outside the site.
  • If two users save new entries at the same moment, the entries may end up with the same number.

before_input ​

  • Type: string
  • Default: ""

Additional text to display before the input field.

after_input ​

  • Type: string
  • Default: ""

Additional text to display after the input field.

Examples ​

Basic Number Field ​

This example demonstrates a simple Number field configuration that allows users to input integer values.

yaml
- name: quantity
  label: Quantity
  widget: number
toml
[[fields]]
name = "quantity"
label = "Quantity"
widget = "number"
json
{
  "name": "quantity",
  "label": "Quantity",
  "widget": "number"
}
js
{
  name: 'quantity',
  label: 'Quantity',
  widget: 'number',
}

Output example:

yaml
quantity: 5
toml
quantity = 5
json
{
  "quantity": 5
}

Price Field ​

This example demonstrates a Number field configured to store floating-point values, suitable for representing prices. It includes a dollar sign before the input field and sets a default value.

yaml
- name: price
  label: Price
  widget: number
  value_type: float
  before_input: $
  default: 9.99
toml
[[fields]]
name = "price"
label = "Price"
widget = "number"
value_type = "float"
before_input = "$"
default = 9.99
json
{
  "name": "price",
  "label": "Price",
  "widget": "number",
  "value_type": "float",
  "before_input": "$",
  "default": 9.99
}
js
{
  name: 'price',
  label: 'Price',
  widget: 'number',
  value_type: 'float',
  before_input: '$',
  default: 9.99,
}

Output example:

yaml
price: 19.99
toml
price = 19.99
json
{
  "price": 19.99
}

Sequential Number ​

This example numbers invoices automatically, starting from 1000, and names each invoice file after its number, e.g. content/invoices/1000.md:

yaml
collections:
  - name: invoices
    label: Invoices
    folder: content/invoices
    create: true
    slug: '{{number}}'
    identifier_field: customer
    fields:
      - name: number
        label: Invoice Number
        widget: number
        auto_increment: true
        min: 1000
      - name: customer
        label: Customer
toml
[[collections]]
name = "invoices"
label = "Invoices"
folder = "content/invoices"
create = true
slug = "{{number}}"
identifier_field = "customer"

[[collections.fields]]
name = "number"
label = "Invoice Number"
widget = "number"
auto_increment = true
min = 1000

[[collections.fields]]
name = "customer"
label = "Customer"
json
{
  "collections": [
    {
      "name": "invoices",
      "label": "Invoices",
      "folder": "content/invoices",
      "create": true,
      "slug": "{{number}}",
      "identifier_field": "customer",
      "fields": [
        {
          "name": "number",
          "label": "Invoice Number",
          "widget": "number",
          "auto_increment": true,
          "min": 1000
        },
        {
          "name": "customer",
          "label": "Customer"
        }
      ]
    }
  ]
}
js
collections: [
  {
    name: 'invoices',
    label: 'Invoices',
    folder: 'content/invoices',
    create: true,
    slug: '{{number}}',
    identifier_field: 'customer',
    fields: [
      {
        name: 'number',
        label: 'Invoice Number',
        widget: 'number',
        auto_increment: true,
        min: 1000,
      },
      {
        name: 'customer',
        label: 'Customer',
      },
    ],
  },
],

Output example of the first invoice:

yaml
number: 1000
customer: Acme
toml
number = 1000
customer = "Acme"
json
{
  "number": 1000,
  "customer": "Acme"
}