> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/pocketbase/pocketbase/llms.txt
> Use this file to discover all available pages before exploring further.

# Text field

> Store string values with optional validation and autogeneration

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

<ParamField path="min" type="int" default="0">
  Minimum required string characters. Set to 0 for no minimum limit.
</ParamField>

<ParamField path="max" type="int" default="5000">
  Maximum allowed string characters. Defaults to 5000 if not set.
</ParamField>

<ParamField path="pattern" type="string">
  Optional regex pattern to match against the field value. Leave empty to skip pattern validation.
</ParamField>

<ParamField path="autogeneratePattern" type="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.
</ParamField>

<ParamField path="required" type="bool" default="false">
  When true, requires the field value to be a non-empty string.
</ParamField>

<ParamField path="primaryKey" type="bool" default="false">
  Marks the field as the primary key. A collection can have only one primary key field, which must be named "id".
</ParamField>

## 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

<Warning>
  When used as a primary key, the field has additional restrictions to ensure filesystem compatibility. Forbidden characters include: `. / \ | " ' \` \< > : ? \* % \$ \000 \t \n \r\` and space.
</Warning>

## Special setter modifiers

The text field supports the autogenerate modifier:

<CodeGroup>
  ```go Autogenerate theme={null}
  // 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"
  ```
</CodeGroup>

## Go examples

<Tabs>
  <Tab title="Basic usage">
    ```go theme={null}
    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")
    ```
  </Tab>

  <Tab title="With pattern validation">
    ```go theme={null}
    field := &core.TextField{
        Name:     "username",
        Required: true,
        Min:      3,
        Max:      20,
        Pattern:  "^[a-z0-9_]+$", // lowercase alphanumeric and underscore only
    }

    collection.Fields.Add(field)

    record.Set("username", "john_doe")
    ```
  </Tab>

  <Tab title="Autogenerate slug">
    ```go theme={null}
    field := &core.TextField{
        Name:                "slug",
        Required:            true,
        AutogeneratePattern: "[a-z0-9]{8}",
        Pattern:             "^[a-z0-9-]+$",
    }

    collection.Fields.Add(field)

    // Autogenerate on create
    record.Set("slug:autogenerate", "article-")
    // Result: "article-x7k2m9p4"
    ```
  </Tab>

  <Tab title="Primary key">
    ```go theme={null}
    field := &core.TextField{
        Name:       "id",
        PrimaryKey: true,
        Required:   true,
        Pattern:    "^[a-z0-9]{15}$",
    }

    collection.Fields.Add(field)
    ```
  </Tab>
</Tabs>

## Database column type

The text field creates different column types based on configuration:

<CodeGroup>
  ```sql Primary key theme={null}
  TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL
  ```

  ```sql Regular field theme={null}
  TEXT DEFAULT '' NOT NULL
  ```
</CodeGroup>

## Best practices

<Note>
  * 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
</Note>

## Zero value

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