Skip to content

Field

The Field class is used to create fields within a resource, user, table, or block.

Basic Methods

MethodDescription
type(string $type)Sets the field type (text, number, date, select, etc.).
label(string $label)Sets the field label.
description(string $description)Adds a field description.
help(string $help)Displays helper text.
default(mixed $default)Sets the default value.
required(bool $required = true)Marks the field as required.
disabled(bool $disabled = true)Disables field editing.
readOnly(bool $readonly = true)Makes the field read-only.
width(int $width = 100)Sets the field width in percentage. Default 100
prefix(string $prefix)Adds a prefix to a field
sufix(string $sufix)Adds a sufix to a field
permissions(array $permissions)Controls where the element is shown based on conditions.

Creating a field:

php
Field::make('name') // Creates a text field with the name 'name'.
php
Field::make('name')->label('Name')
// or even shorter
Field::make('Name')

Types

  • text (default)
  • textarea
  • richtext
  • editorjs
  • ace
  • select
  • resourcelist
  • boolean
  • relationship
  • currency
  • number
  • toggle
  • checkbox
  • radio
  • slug
  • heading
  • imask
  • tag
  • file
  • image
  • video
  • button
  • map
  • gallery
  • datetime
  • date
  • time
  • keyvalue
  • jsongrid
  • table
  • colorpalette
  • colorpicker
  • fieldset
  • readonly

TEXTAREA (textarea), RICHTEXT (richtext)

MethodDescription
height(int $height)Sets the field height.
php
Field::make('Content')
    ->type('textarea')
    ->height(200)

HEADING (heading)

MethodDescription
html(string $html)Allows inserting HTML into the heading.
php
Field::make('Description')
    ->type('heading')
    ->html('<p>Custom text</p>')

Input Mask (imask)

MethodDescription
maskOptions(string $maskOptions)Sets input mask parameters.
php
Field::make('Phone')
    ->type('imask')
    ->maskOptions("{
        mask: '+{38} (000) 000-00-00'
    }")
php
Field::make('Date')
    ->type('imask')
    ->maskOptions("{
        mask: Date,
        pattern: 'd{.}`m{.}`Y',
        blocks: {
            d: { mask: IMask.MaskedRange, from: 1, to: 31 },
            m: { mask: IMask.MaskedRange, from: 1, to: 12 },
            Y: { mask: IMask.MaskedRange, from: 1900, to: 2099 }
        }
    }")
php
Field::make('card')
    ->type('imask')
    ->label('Credit Card Number')
    ->maskOptions("{
        mask: '0000 0000 0000 0000'
    }}")

Number (number)

MethodDescription
allowDecimals(bool $allowDecimals = true)Enables decimal numbers.
decimalSeparator(string $decimalSeparator)Sets the decimal separator.
decimalPrecision(int $decimalPrecision)Defines the number of decimal places.
allowNegative(bool $allowNegative = true)Allows negative values.
minValue(int $minValue)Sets the minimum value.
maxValue(int $maxValue)Sets the maximum value.
php
Field::make('price')
    ->type('number')
    ->label('Price')
    ->minValue(100)
    ->maxValue(200)

Currency (currency)

MethodDescription
currency(string $currency)Currency. For example USD
minValue(int $minValue)Sets the minimum value.
maxValue(int $maxValue)Sets the maximum value.

SELECT (select)

MethodDescription
multiple(bool $multiple = true)Enables multiple selection.
modelClass(string $modelClass)Specifies the model class for loading data.
fieldName(string $fieldName)
displayField(string $displayField)Defines the field for displaying values.
valueField(string $valueField)Specifies the field for retrieving values.
where(array $where)Sets filtering conditions for data retrieval.
sortby(string $sortby)Defines the sorting field.
sortdir(string $sortdir)Sets the sorting direction (ASC or DESC).
limit(int $limit)Limits the number of records per page.
options($options)Sets a static list of selectable values.

Example with an array:

php
Field::make('status')
    ->type('select')
    ->options([
        'active' => 'Active',
        'inactive' => 'Inactive',
        'pending' => 'Pending'
    ])

Or using a generator function:

php
Field::make('resource')
    ->type('select')
    ->options(function() {
        $modx = self::getModx();
        $resources = $modx->getCollection(\modResource::class, [
            'published' => true,
        ]);
    
        return array_column($resources, 'pagetitle', 'id');
    })
php
Field::make('Category')
    ->type('select')
    ->multiple()
    ->modelClass(\pbResource::class)
    ->fieldName('categories')
    ->displayField('pagetitle')
    ->valueField('alias')
    ->required(),

DATETIME (datetime or date)

MethodDescription
storage(string $storage)Storage format: timestamp (Unix) or datetime (Y-m-d H:i:s)
hiddenFormat(string $hiddenFormat)Sets the hidden date format.
dateFormat(string $dateFormat)Defines the display date format.
startDay(int $startDay)Sets the first day of the week (0 – Sunday, 1 – Monday).
minDateValue(int $minDateValue)Sets the minimum allowed date.
maxDateValue(int $maxDateValue)Sets the maximum allowed date.
hideTime(bool $hideTime = true)Hides the time selection.
timeFormat(string $timeFormat)Defines the time format.
timeIncrement(int $timeIncrement)Sets the time step (e.g., every 15 minutes).
minTimeValue(string $minTimeValue)Defines the minimum allowed time.
maxTimeValue(string $maxTimeValue)Defines the maximum allowed time.
disabledDates(array $disabledDates)Specifies a list of unavailable dates.
disabledDays(array $disabledDays)Blocks specific days of the week (0 – Sunday, 1 – Monday, etc.).
php
Field::make('date')
    ->label('Date')
    ->type('date')
    ->storage('timestamp')
    ->dateFormat('Y-m-d, H:i')
    ->required()
    ->default(time()),
MethodDescription
source(int $source)Sets the file source.
sourcePath(string $sourcePath)Defines the file path.
openTo(string $openTo)Specifies the folder to open in the file manager.
thumbnails(string $thumbnails)Defines the image thumbnail template. Only for gallery
php
Field::make('gallery')
    ->label('Images')
    ->type('gallery')
    ->source(1)
    ->sourcePath('/assets/images/')
    ->thumbnails('{"webp":{"w":120,"h":90,"q":90,"zc":"1","bg":"000000","f":"webp"}}')

KEYVALUE (keyvalue)

MethodDescription
keyLabel(string $keyLabel)Sets the label for the key.
valueLabel(string $valueLabel)Sets the label for the value.
php
Field::make('Options')
    ->type('keyvalue')
    ->keyLabel('Name')
    ->valueLabel('Alias')

JSONGRID (jsongrid)

MethodDescription
fields(array $fields)Defines a list of fields.
php
Field::make('Options')
    ->type('jsongrid')
    ->fields([
        Field::make('Title'), 
        Field::make('Desc'), 
        Field::make('Price'), 
    ])

FIELDSET (fieldset)

MethodDescription
fields(array $fields)Defines a list of fields.
php
Field::make('Options')
    ->type('fieldset')
    ->fields([
        Field::make('Title'), 
        Field::make('Desc'), 
        Field::make('Price'), 
    ])

MAP (map)

MethodDescription
center(string $center)Sets the map center
zoom(int $zoom)Sets the zoom level
layer(string $layer)Sets the map style
icon(string $icon)Sets the marker icon
iconSize(string $iconSize)Sets the icon size
php
Field::make('Map')
    ->type('map')
    ->center('50.4500336,30.5241361')
    ->layer('dark')
    ->icon('/assets/images/logo.svg')
    ->iconSize('25,25')
    ->zoom(6)
    ->required()

MAP LEAFLET

MethodDescription
zoomControl(bool $zoomControl = true)Zoom control element
closePopupOnClick(bool $closePopupOnClick = true)Closes pop-up windows when clicking on a map
doubleClickZoom(bool $doubleClickZoom = true)Zooms the map when you double-click the mouse
dragging(bool $dragging = true)Drag and drop a map using the mouse or touch
keyboard(bool $keyboard = true)Controlling the map with the keyboard
scrollWheelZoom(bool $scrollWheelZoom = true)Zooms the map with the mouse wheel
touchZoom(bool $touchZoom = true)Zooms the map by touch-dragging with two fingers

TOGGLE (toggle)

MethodDescription
danger(bool $danger = true)Red toggle
warning(bool $warning = true)Yellow toggle
php
Field::make('active')
    ->label('Active resource')
    ->type('toggle')
    ->danger()

CHECKBOX (checkbox), RADIO (radio)

MethodDescription
checked(bool $checked = true)Marks the checkbox as checked
multiple(bool $multiple = true)Enables a group of checkboxes
columns(int $columns)Number of checkboxes per row
options($options)Sets a static list of selectable values
php
Field::make('Brand')
    ->type('checkbox')
    ->multiple()
    ->columns(3)
    ->options([
        "nike" => "Nike",
        "adidas" => "Adidas",
        "zara" => "Zara",
        "h&m" => "H&M",
        "gucci" => "Gucci",
        "louis vuitton" => "Louis Vuitton",
        "prada" => "Prada",
        "burberry" => "Burberry",
        "uniqlo" => "Uniqlo"
    ])
    ->required(),

TAG (tag)

MethodDescription
options($options)Sets a static list of selectable values
php
Field::make('Brand')
    ->type('tag')
    ->options([
        "nike" => "Nike",
        "adidas" => "Adidas",
        "zara" => "Zara",
    ])

TABLE (table)

MethodDescription
resource(bool $resource = true)Creates a resource table (pbResource)
searchable(bool $searchable = true)Enables search in the table
pagination(bool $pagination = true)Enables pagination. true by default
pageSize(int $pageSize = 20)Number of items per page
fields(array $fields)Defines the list of fields inside the table
columns($columns)Defines table columns
filters(array $filters)Adds filters to the table
php
Field::make('products')
    ->type('table')
    ->resource()
    ->pageSize(10)
    ->fields([
        Field::make('name')->label('Name'),
        Field::make('price')->type('number')->label('Price'),
        Field::make('stock')->type('number')->label('Stock')
    ])
    ->columns([
        Column::make('Name'),
        Column::make('Price'),
        Column::make('Stock'),
    ])
    ->filters([
       Filter::make('Price') 
    ])

RELATIONSHIP (relationship)

Inherits methods from the SELECT field and adds its own:

MethodDescription
relationType(string $relationType)Type of relation: one_to_one, one_to_many, many_to_many, many_to_one
primaryTable(string $primaryTable)Primary table: pbBlockValue, pbTableValue, pbResource, pbUser
relatedTable(string $relatedTable)Related table: pbBlockValue, pbTableValue, pbResource, pbUser
php
Field::make('Category')
    ->label('Categories')
    ->type('relationship')
    ->relationType('many_to_many')
    ->primaryTable(\pbResource::class)
    ->relatedTable(\pbTableValue::class)
    ->fieldName('categories')
    ->displayField('pagetitle')
    ->valueField('id'),

SLUG (slug)

MethodDescription
from(string $from)Depends on another field
separator(string $separator)Separator. Defaults to -
php
Field::make('Title')
Field::make('Alias')
    ->type('slug')
    ->from('title')

Field Dependencies

  • hidden(string $field, string $operator, mixed $value) – Hides the current field based on another field’s value.
    • $field – Name of the dependent field.
    • $operator – Comparison operator (>, <, >=, <=, =, !=, ><, !><, IN, NOT IN).
    • $value – Value to compare against.
php
Field::make('discount')
    ->type('number')
    ->hidden('has_discount', '!=', 1)

In this case, the discount field will be hidden if has_discount is not equal to 1.

© PageBlocks 2019-present