Skip to content

Field

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

Creating a field:

php
Field::make('name'); // Creates a text field with the name 'name'.

To set a field label, use the label method:

php
Field::make('name')->label('Name');
// or even shorter
Field::make('Name');

Basic Methods

These methods allow you to configure the main field parameters:

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

Text Fields (textarea, richtext)

  • height(int $height) – Sets the field height (only for textarea and richtext).

Heading (heading)

  • html(string $html) – Allows inserting HTML into the heading.

Input Mask (imask)

  • options(string $options) – Sets input mask parameters.

Number Field (number)

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

  • multiple(bool $multiple = true) – Enables multiple selection.
  • model(string $model) – Specifies the model for loading data.
  • 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.
  • values(array|callable $values) – Sets a static list of selectable values.

Example with an array:

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

Or using a generator function:

php
Field::make('resource')->values(function() {
    $modx = self::getModx();
    $resources = $modx->getCollection(\modResource::class, [
        'published' => true,
    ]);

    return array_column($resources, 'pagetitle', 'id');
});

Date & Time (datetime)

  • 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.).

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

Example:

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"}}');

Key-Value (keyvalue)

  • keyLabel(string $keyLabel) – Sets the label for the key.
  • valueLabel(string $valueLabel) – Sets the label for the value.

Table (table)

  • columns($columns) – Defines table columns.
  • searchable(bool $searchable = true) – Enables search within the table.
  • filters(array $filters) – Adds filters to the table.
  • fields(array $fields) – Defines the list of fields inside the table.

Example:

php
Field::make('products')->fields([
    Field::make('name')->label('Name'),
    Field::make('price')->type('number')->label('Price'),
    Field::make('stock')->type('number')->label('Stock')
]);

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.

Example:

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