Skip to main content
The text field stores string values and supports validation through patterns, length constraints, and autogeneration capabilities. You can use it for simple text inputs, slugs, or as a primary key field.

Configuration options

min
int
default:"0"
Minimum required string characters. Set to 0 for no minimum limit.
max
int
default:"5000"
Maximum allowed string characters. Defaults to 5000 if not set.
pattern
string
Optional regex pattern to match against the field value. Leave empty to skip pattern validation.
autogeneratePattern
string
Optional regex pattern used to generate random strings automatically on record create if no explicit value is set. The generated value must satisfy min, max, and pattern constraints.
required
bool
default:"false"
When true, requires the field value to be a non-empty string.
primaryKey
bool
default:"false"
Marks the field as the primary key. A collection can have only one primary key field, which must be named “id”.

Validation rules

The text field validates:
  • Length: Value must be between min and max characters (counting multi-byte characters as one)
  • Pattern: If specified, value must match the regex pattern
  • Required: If enabled, value cannot be empty
  • Primary key: Additional filesystem-safe character restrictions apply
When used as a primary key, the field has additional restrictions to ensure filesystem compatibility. Forbidden characters include: . / \ | " ' \ < > : ? * % $ \000 \t \n \r` and space.

Special setter modifiers

The text field supports the autogenerate modifier:
// Autogenerate a value using the pattern
record.Set("slug:autogenerate", "")
// Result: random value like "abc123xyz"

// Autogenerate with prefix
record.Set("slug:autogenerate", "prefix-")
// Result: "prefix-abc123xyz"

Go examples

import "github.com/pocketbase/pocketbase/core"

field := &core.TextField{
    Name:     "title",
    Required: true,
    Min:      3,
    Max:      100,
}

// Use in a collection
collection.Fields.Add(field)

// Set field value
record.Set("title", "My Article Title")

Database column type

The text field creates different column types based on configuration:
TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL

Best practices

  • Use autogeneratePattern for fields like slugs that need unique identifiers
  • Test your regex patterns thoroughly to ensure they produce valid values
  • For primary keys, stick to URL-safe characters for better compatibility
  • Consider using reasonable max values to prevent database bloat

Zero value

The zero value for text fields is an empty string "".