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

# Fields reference

> Complete guide to all field types available in PocketBase collections

Fields define the structure and validation rules for data in your PocketBase collections. Each field type is optimized for specific data formats and use cases.

## Common field properties

All field types share these core properties:

```go theme={null}
type Field interface {
    Name   string  // Unique field name (used in API)
    Id     string  // Stable identifier (auto-generated)
    System bool    // System fields cannot be renamed/removed
    Hidden bool    // Hidden fields are excluded from API responses
}
```

### Additional common properties

Most fields also support:

* **Presentable** - Hints the dashboard to use this field in record previews
* **Required** - Makes the field value mandatory (non-empty)

<Info>
  Field IDs are automatically generated when you add fields to a collection. They ensure stability even if you rename the field.
</Info>

## Text field

Stores string values with optional validation constraints.

```go theme={null}
&core.TextField{
    Name:     "title",
    Required: true,
    Min:      3,
    Max:      200,
    Pattern:  "^[a-zA-Z0-9 ]+$",
}
```

### Properties

* **Min** - Minimum character length (0 = no limit)
* **Max** - Maximum character length (0 = default 5000)
* **Pattern** - Regular expression for validation
* **AutogeneratePattern** - Regex pattern for generating random values
* **PrimaryKey** - Mark as collection primary key (only for `id` field)

### Special modifiers

```go theme={null}
// Autogenerate with prefix
record.Set("slug:autogenerate", "post-")  // "post-abc123xyz"
```

### Examples

<Tabs>
  <Tab title="Username">
    ```go theme={null}
    &core.TextField{
        Name:     "username",
        Required: true,
        Min:      3,
        Max:      30,
        Pattern:  "^[a-zA-Z0-9_]+$",
    }
    ```
  </Tab>

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

  <Tab title="Short description">
    ```go theme={null}
    &core.TextField{
        Name: "tagline",
        Max:  140,
    }
    ```
  </Tab>
</Tabs>

## Number field

Stores numeric values (stored as float64 internally).

```go theme={null}
&core.NumberField{
    Name:     "price",
    Required: true,
    Min:      core.Float64(0),
    Max:      core.Float64(999999.99),
}
```

### Properties

* **Min** - Minimum value (nil = no limit)
* **Max** - Maximum value (nil = no limit)
* **OnlyInt** - Restrict to integer values only
* **Required** - Require non-zero value

### Special modifiers

```go theme={null}
// Increment/decrement
record.Set("views+", 1)      // Add 1 to current value
record.Set("stock-", 5)      // Subtract 5 from current value
```

### Examples

<Tabs>
  <Tab title="Price">
    ```go theme={null}
    &core.NumberField{
        Name:     "price",
        Required: true,
        Min:      core.Float64(0),
        Max:      core.Float64(1000000),
    }
    ```
  </Tab>

  <Tab title="Quantity (integer)">
    ```go theme={null}
    &core.NumberField{
        Name:     "quantity",
        Required: true,
        Min:      core.Float64(0),
        OnlyInt:  true,
    }
    ```
  </Tab>

  <Tab title="Rating">
    ```go theme={null}
    &core.NumberField{
        Name: "rating",
        Min:  core.Float64(0),
        Max:  core.Float64(5),
    }
    ```
  </Tab>
</Tabs>

## Email field

Stores and validates email addresses.

```go theme={null}
&core.EmailField{
    Name:          "email",
    Required:      true,
    OnlyDomains:   []string{"company.com"},
    ExceptDomains: []string{"spam.com"},
}
```

### Properties

* **OnlyDomains** - Whitelist of allowed email domains
* **ExceptDomains** - Blacklist of forbidden email domains
* **Required** - Require non-empty email

<Note>
  You can only use **either** `OnlyDomains` or `ExceptDomains`, not both.
</Note>

### Examples

<Tabs>
  <Tab title="Basic email">
    ```go theme={null}
    &core.EmailField{
        Name:     "contact_email",
        Required: true,
    }
    ```
  </Tab>

  <Tab title="Corporate only">
    ```go theme={null}
    &core.EmailField{
        Name:        "work_email",
        Required:    true,
        OnlyDomains: []string{"company.com", "subsidiary.com"},
    }
    ```
  </Tab>

  <Tab title="Block disposable">
    ```go theme={null}
    &core.EmailField{
        Name:          "email",
        Required:      true,
        ExceptDomains: []string{"tempmail.com", "throwaway.email"},
    }
    ```
  </Tab>
</Tabs>

## URL field

Stores and validates URLs.

```go theme={null}
&core.URLField{
    Name:         "website",
    Required:     true,
    OnlyDomains:  []string{"example.com"},
    ExceptDomains: []string{"blocked.com"},
}
```

### Properties

* **OnlyDomains** - Whitelist of allowed domains
* **ExceptDomains** - Blacklist of forbidden domains
* **Required** - Require non-empty URL

## Bool field

Stores true/false values.

```go theme={null}
&core.BoolField{
    Name: "published",
}
```

### Properties

Only the common properties (Name, Required is not applicable as false is a valid value).

## Date field

Stores date and time values.

```go theme={null}
&core.DateField{
    Name:     "published_at",
    Required: true,
    Min:      "2024-01-01 00:00:00.000Z",
    Max:      "2025-12-31 23:59:59.999Z",
}
```

### Properties

* **Min** - Minimum date (RFC 3339 format)
* **Max** - Maximum date (RFC 3339 format)
* **Required** - Require non-empty date

## Autodate field

Automatically sets date on create and/or update.

```go theme={null}
&core.AutodateField{
    Name:     "created",
    OnCreate: true,
    OnUpdate: false,
}
```

### Properties

* **OnCreate** - Set date when record is created
* **OnUpdate** - Update date when record is modified

### Examples

<Tabs>
  <Tab title="Created timestamp">
    ```go theme={null}
    &core.AutodateField{
        Name:     "created_at",
        OnCreate: true,
        OnUpdate: false,
    }
    ```
  </Tab>

  <Tab title="Updated timestamp">
    ```go theme={null}
    &core.AutodateField{
        Name:     "updated_at",
        OnCreate: true,
        OnUpdate: true,
    }
    ```
  </Tab>
</Tabs>

## Select field

Stores single or multiple predefined values.

```go theme={null}
&core.SelectField{
    Name:      "status",
    Required:  true,
    Values:    []string{"draft", "published", "archived"},
    MaxSelect: 1,
}
```

### Properties

* **Values** - List of allowed values
* **MaxSelect** - Max selections (1 = single, >1 = multiple)
* **Required** - Require at least one selection

### Examples

<Tabs>
  <Tab title="Single select">
    ```go theme={null}
    &core.SelectField{
        Name:      "status",
        Required:  true,
        Values:    []string{"active", "inactive", "pending"},
        MaxSelect: 1,
    }
    ```
  </Tab>

  <Tab title="Multiple select">
    ```go theme={null}
    &core.SelectField{
        Name:      "tags",
        Values:    []string{"tech", "business", "lifestyle", "travel"},
        MaxSelect: 3,
    }
    ```
  </Tab>
</Tabs>

## JSON field

Stores arbitrary JSON data.

```go theme={null}
&core.JSONField{
    Name:     "metadata",
    MaxSize:  2097152, // 2MB
}
```

### Properties

* **MaxSize** - Maximum JSON size in bytes (default 2MB)
* **Required** - Require non-empty JSON

### Example

```go theme={null}
&core.JSONField{
    Name:    "settings",
    MaxSize: 1048576, // 1MB
}

// Usage
record.Set("settings", map[string]any{
    "theme": "dark",
    "notifications": true,
    "language": "en",
})
```

## Editor field

Stores rich text HTML content.

```go theme={null}
&core.EditorField{
    Name:             "content",
    Required:         true,
    ConvertURLs:      true,
    ExceptDomains:    []string{"malicious.com"},
    OnlyDomains:      []string{"trusted.com"},
}
```

### Properties

* **ConvertURLs** - Auto-convert plain text URLs to links
* **OnlyDomains** - Whitelist of allowed domains in content
* **ExceptDomains** - Blacklist of forbidden domains
* **Required** - Require non-empty content

## File field

Handles file uploads with validation.

```go theme={null}
&core.FileField{
    Name:      "attachments",
    MaxSelect: 5,
    MaxSize:   5242880, // 5MB per file
    MimeTypes: []string{"image/jpeg", "image/png", "application/pdf"},
    Thumbs:    []string{"100x100", "300x300", "0x500"},
    Protected: false,
}
```

### Properties

* **MaxSelect** - Max files (1 = single, >1 = multiple)
* **MaxSize** - Max size per file in bytes (default 5MB)
* **MimeTypes** - Allowed MIME types (empty = all)
* **Thumbs** - Thumbnail sizes for images
* **Protected** - Require token to access files
* **Required** - Require at least one file

### Thumbnail formats

* `100x300` - Crop to 100x300 from center
* `100x300t` - Crop to 100x300 from top
* `100x300b` - Crop to 100x300 from bottom
* `100x300f` - Fit inside 100x300 (no crop)
* `0x300` - Resize to 300px height, preserve aspect ratio
* `100x0` - Resize to 100px width, preserve aspect ratio

### Special modifiers

```go theme={null}
// Append files
record.Set("images+", []*filesystem.File{newImage})

// Prepend files
record.Set("+images", []*filesystem.File{newImage})

// Remove files
record.Set("images-", "old_image.jpg")
```

### Examples

<Tabs>
  <Tab title="Profile picture">
    ```go theme={null}
    &core.FileField{
        Name:      "avatar",
        MaxSelect: 1,
        MaxSize:   2097152, // 2MB
        MimeTypes: []string{"image/jpeg", "image/png", "image/webp"},
        Thumbs:    []string{"100x100", "300x300"},
    }
    ```
  </Tab>

  <Tab title="Documents">
    ```go theme={null}
    &core.FileField{
        Name:      "documents",
        MaxSelect: 10,
        MaxSize:   10485760, // 10MB
        MimeTypes: []string{
            "application/pdf",
            "application/msword",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        },
        Protected: true,
    }
    ```
  </Tab>

  <Tab title="Product images">
    ```go theme={null}
    &core.FileField{
        Name:      "images",
        Required:  true,
        MaxSelect: 5,
        MaxSize:   5242880, // 5MB
        MimeTypes: []string{"image/jpeg", "image/png"},
        Thumbs:    []string{"100x100", "400x400", "1200x0"},
    }
    ```
  </Tab>
</Tabs>

## Relation field

Links to records in other collections.

```go theme={null}
&core.RelationField{
    Name:          "author",
    Required:      true,
    CollectionId:  "users_collection_id",
    MaxSelect:     1,
    MinSelect:     1,
    CascadeDelete: false,
}
```

### Properties

* **CollectionId** - ID of the related collection (required)
* **MaxSelect** - Max related records (1 = single, >1 = multiple)
* **MinSelect** - Min required relations
* **CascadeDelete** - Delete record if all relations are deleted
* **Required** - Require at least one relation

<Warning>
  **CollectionId cannot be changed** after the field is created. You must delete and recreate the field to change the target collection.
</Warning>

### Special modifiers

```go theme={null}
// Append relations
record.Set("tags+", []string{"tag1_id", "tag2_id"})

// Prepend relations
record.Set("+tags", []string{"tag1_id"})

// Remove relations
record.Set("tags-", "tag1_id")
```

### Examples

<Tabs>
  <Tab title="Single relation">
    ```go theme={null}
    &core.RelationField{
        Name:         "category",
        Required:     true,
        CollectionId: categoryCollectionId,
        MaxSelect:    1,
    }
    ```
  </Tab>

  <Tab title="Multiple relations">
    ```go theme={null}
    &core.RelationField{
        Name:         "tags",
        CollectionId: tagsCollectionId,
        MaxSelect:    10,
        MinSelect:    1,
    }
    ```
  </Tab>

  <Tab title="Self-referencing">
    ```go theme={null}
    &core.RelationField{
        Name:         "parent",
        CollectionId: collection.Id, // Same collection
        MaxSelect:    1,
    }
    ```
  </Tab>
</Tabs>

## Password field

Stores hashed passwords (auth collections only).

```go theme={null}
&core.PasswordField{
    Name:     "password",
    System:   true,
    Hidden:   true,
    Required: true,
    Min:      8,
    Max:      100,
    Pattern:  "", // Optional pattern for validation
}
```

### Properties

* **Min** - Minimum password length
* **Max** - Maximum password length
* **Pattern** - Regex pattern for validation
* **Required** - Require non-empty password

<Info>
  Passwords are automatically hashed using bcrypt before storage. The plain text password is never stored.
</Info>

## Working with fields

### Adding fields to collections

```go theme={null}
collection.Fields.Add(
    &core.TextField{Name: "title"},
    &core.TextField{Name: "content"},
)
```

### Adding at specific position

```go theme={null}
// Insert at index 1
collection.Fields.AddAt(1,
    &core.TextField{Name: "subtitle"},
)
```

### Removing fields

```go theme={null}
collection.Fields.RemoveByName("old_field")
collection.Fields.RemoveById("field_id")
```

### Getting fields

```go theme={null}
field := collection.Fields.GetByName("title")
field := collection.Fields.GetById("field_id")

// Get all field names
names := collection.Fields.FieldNames()

// Get as map
fieldsMap := collection.Fields.AsMap()
```

### Field validation

Fields are validated when you save the collection:

```go theme={null}
if err := app.Save(collection); err != nil {
    // Handle validation errors
    if validationErr, ok := err.(validation.Errors); ok {
        for field, fieldErr := range validationErr {
            fmt.Printf("Field %s: %v\n", field, fieldErr)
        }
    }
}
```

## Helper functions

Common helper functions for working with field values:

```go theme={null}
// For pointer fields (Min, Max in NumberField)
core.Float64(42.5)    // Returns *float64
core.String("value")  // Returns *string

// For checking field types
if textField, ok := field.(*core.TextField); ok {
    // Work with text field
}
```

## Best practices

<Tabs>
  <Tab title="Validation">
    * Set appropriate min/max constraints
    * Use patterns for format validation
    * Mark fields as required only when necessary
    * Provide sensible default values
  </Tab>

  <Tab title="Naming">
    * Use camelCase or snake\_case consistently
    * Keep names descriptive but concise
    * Avoid reserved names (id, created, updated)
    * Use plural for multiple select fields
  </Tab>

  <Tab title="Performance">
    * Use indexes for frequently queried fields
    * Limit relation depth (avoid deep nesting)
    * Set appropriate MaxSize for files and JSON
    * Use select fields instead of text for fixed values
  </Tab>

  <Tab title="Security">
    * Mark sensitive fields as Hidden
    * Use Protected for private files
    * Validate file MIME types
    * Set appropriate size limits
  </Tab>
</Tabs>

## Next steps

<CardGroup cols={2}>
  <Card title="Base collections" icon="database" href="/collections/base-collections">
    Create your first collection
  </Card>

  <Card title="Auth collections" icon="user-lock" href="/collections/auth-collections">
    Add user authentication
  </Card>

  <Card title="Validation" icon="check" href="/concepts/api-rules">
    Advanced validation techniques
  </Card>

  <Card title="API rules" icon="shield" href="/concepts/api-rules">
    Secure your data
  </Card>
</CardGroup>
