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

String Field

Enter and manage short text values in Sveltia CMS for content entries.

The String field type allows users to input and manage short to medium-length text strings within the CMS entry form.

User Interface

Editor

Single-line text input field for entering short to medium-length strings. It supports standard text input features like copy-paste, undo-redo and basic keyboard shortcuts.

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

A character counter can be displayed if the minlength or maxlength option is set, and a user-friendly validation message will appear if the input does not meet the specified length requirements. The counter only counts what the user types: the prefix and suffix are not included.

Emoji autocomplete is enabled by default, unless the type option is url or email. Typing a colon followed by one or more characters, such as :smi, brings up a list of matching emojis, the same way it works on GitHub, Slack and other apps. Use the arrow keys to move through the list, the Enter or Tab key to insert the selected emoji, and the Escape key to dismiss the list. This can be turned off with the use_emoji_autocomplete option.

Preview

A read-only view of the entered string. If the prefix or suffix options are set, they will be displayed along with the string in the preview.

If the string is a YouTube video URL, it will be automatically embedded in the preview for better visualization.

If the string is a regular URL, it will be displayed as a clickable link that opens in a new browser tab.

Data Type

A string. If the required option is set to false and the field is left empty, the value will be an empty string.

Data Validation

  • If the required option is set to true, the string must not be empty.
  • If the minlength and/or maxlength options are specified, the string length must be within the defined limits. The length doesn’t include the prefix and suffix, or leading and trailing whitespace, and an emoji counts as a single character.
  • If the type option is url or email, the string must be a valid URL or email address, checked without the prefix and suffix. An email address must also have a dot in its domain name.
  • If the pattern option is provided, the string must match the specified regular expression pattern.

Options

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

Optional Options

widget

  • Type: string
  • Default: string

Must be set to string, but is optional since it is the default field type.

default

  • Type: string
  • Default: ""

The default value for the field. It’s used when creating a new entry, and when opening an existing entry that doesn’t have the field, such as one written before the field was added to the configuration.

type

  • Type: string
  • Default: ""

The value type, either url or email. This option shows the matching keyboard on mobile devices, and also enables basic validation for URL or email format.

prefix

  • Type: string
  • Default: ""

Strings to be prepended to the value when saving or displaying it. If the value is empty, the prefix will not be added.

suffix

  • Type: string
  • Default: ""

Strings to be appended to the value when saving or displaying it. If the value is empty, the suffix will not be added.

minlength

  • Type: integer
  • Default: 0

Minimum length of the string, not counting the prefix and suffix. This enables a character counter in the UI and validation.

maxlength

  • Type: integer
  • Default: Infinity

Maximum length of the string, not counting the prefix and suffix. This enables a character counter in the UI and validation.

before_input

  • Type: string
  • Default: ""

Text to display before the input field.

after_input

  • Type: string
  • Default: ""

Text to display after the input field.

use_emoji_autocomplete

  • Type: boolean
  • Default: true, unless the type option is url or email

Whether to enable emoji autocomplete in the text input. When set to true, typing a colon followed by one or more characters, such as :smi, brings up a list of matching emojis that can be inserted into the field.

The colon must be at the beginning of the input or preceded by a space or an opening bracket. A colon in the middle of a word, as in 12:34, does not trigger the suggestions.

Examples

Basic String Field

The following example demonstrates a basic String field for entering a title. Note that the widget option is optional since string is the default field type.

- name: title
  label: Title
[[fields]]
name = "title"
label = "Title"
{
  "name": "title",
  "label": "Title"
}
{
  name: "title",
  label: "Title",
}

Output example:

title: My First Post
title = "My First Post"
{
  "title": "My First Post"
}

Minimum and Maximum Length

The following example demonstrates a String field with minlength and maxlength options set to enforce input length constraints. It also includes a default value.

- name: title
  label: Title
  widget: string
  default: 'Enter your title here.'
  minlength: 5
  maxlength: 100
[[fields]]
name = "title"
label = "Title"
widget = "string"
default = "Enter your title here."
minlength = 5
maxlength = 100
{
  "name": "title",
  "label": "Title",
  "widget": "string",
  "default": "Enter your title here.",
  "minlength": 5,
  "maxlength": 100
}
{
  name: "title",
  label: "Title",
  widget: "string",
  default: "Enter your title here.",
  minlength: 5,
  maxlength: 100,
}

Output example:

title: My Second Post
title = "My Second Post"
{
  "title": "My Second Post"
}

URL Field

The following example demonstrates a String field configured for URL input. Validation will ensure that the entered value is a properly formatted URL.

- name: website
  label: Website
  widget: string
  type: url
[[fields]]
name = "website"
label = "Website"
widget = "string"
type = "url"
{
  "name": "website",
  "label": "Website",
  "widget": "string",
  "type": "url"
}
{
  name: "website",
  label: "Website",
  widget: "string",
  type: "url",
}

Output example:

website: https://example.com
website = "https://example.com"
{
  "website": "https://example.com"
}

Email Field with Prefix and Suffix

Some use cases may require adding specific text before or after the input value, such as mailto: for email links or query parameters. The following example demonstrates a String field configured for email input with prefix and suffix options.

- name: contact_email_link
  label: Contact Email Link
  widget: string
  type: email
  prefix: 'mailto:'
  suffix: '?subject=Inquiry'
[[fields]]
name = "contact_email_link"
label = "Contact Email Link"
widget = "string"
type = "email"
prefix = "mailto:"
suffix = "?subject=Inquiry"
{
  "name": "contact_email_link",
  "label": "Contact Email Link",
  "widget": "string",
  "type": "email",
  "prefix": "mailto:",
  "suffix": "?subject=Inquiry"
}
{
  name: "contact_email_link",
  label: "Contact Email Link",
  widget: "string",
  type: "email",
  prefix: "mailto:",
  suffix: "?subject=Inquiry",
}

Output example:

contact_email_link: mailto:contact@example.com?subject=Inquiry
contact_email_link = "mailto:contact@example.com?subject=Inquiry"
{
  "contact_email_link": "mailto:contact@example.com?subject=Inquiry"
}

Alternatively, you can use a Compute field to generate the full email link based on a separate email String field, as shown in the Compute field documentation.

Hashtag Field

The following example demonstrates a String field configured for entering hashtags, with a # symbol displayed before the input field and a hint to guide users. Unlike the prefix option, which adds text to the saved value, the before_input option only affects the UI display, not the stored data.

- name: hashtag
  label: Hashtag
  widget: string
  before_input: '#'
  hint: 'Enter a hashtag without the # symbol.'
[[fields]]
name = "hashtag"
label = "Hashtag"
widget = "string"
before_input = "#"
hint = "Enter a hashtag without the # symbol."
{
  "name": "hashtag",
  "label": "Hashtag",
  "widget": "string",
  "before_input": "#",
  "hint": "Enter a hashtag without the # symbol."
}
{
  name: "hashtag",
  label: "Hashtag",
  widget: "string",
  before_input: "#",
  hint: "Enter a hashtag without the # symbol.",
}

Output example:

hashtag: travel
hashtag = "travel"
{
  "hashtag": "travel"
}

Was this page helpful?