Mainstay GitHub
All docs

Fields

The field attributes a type is declared with, what each stores, and the arguments they share.

A field is an attribute on a typed property. The attribute is the field type itself: it knows its column, its validation rules, its JSON Schema, how it casts both ways and the component that draws it in the admin. The property's own type says the rest, so #[Text] public ?string $subtitle is an optional string called subtitle. One declaration therefore gives the field its column, its form field, its validation rules, its JSON Schema and its PHP type; the column still needs a migration in production.

The arguments every field takes

  • required: makes a write need a value. Left out, it follows the property: a nullable property is optional, any other required. Two exceptions: a #[Boolean] is required only with required: true, and a #[Relation] or #[Terms] that holds a list is required only with required: true, since an empty list is a list holding none.

  • localized: gives the field a value per language, kept in the type's _locales table.

  • label: is what the admin calls it, when the property's name does not say it well.

The field types

#[Text] A line of text, `max:` 255 by default: the column is as wide as the rule.
#[Textarea] Long plain text, kept as written.
#[Number] An int or a float, as the property says, with `min:` and `max:`.
#[Boolean] True or false. `required: true` means it must be true.
#[Date] A day, or a moment in UTC with `time: true`.
#[Select] One of `options:`, a list, value to label, or a backed enum.
#[RichText] A document of headings, paragraphs, lists, quotes and code.
#[Image] An image from the library, at the `sizes:` it declares.
#[Relation] One entry or a list of them, of the types in `to:`.
#[Terms] An entry's terms from the taxonomy in `of:`.

An example

app/Content/Event.php
#[Route('/events/{slug}')]
class Event extends Entry
{
    #[Text(required: true, localized: true)]
    public string $slug;

    #[Date(time: true)]
    public ?CarbonImmutable $startsAt;

    #[Number(min: 0)]
    public ?float $price;

    #[Select(options: ['talk' => 'Talk', 'workshop' => 'Workshop'])]
    public string $kind = 'talk';

    #[Boolean]
    public bool $soldOut = false;

    #[Internal]
    #[Textarea]
    public ?string $notes;
}

A default on the property is what a new row holds when nothing is given. Each field type checks the property it sits on when the type is first read, so #[Date] public string $when is refused with a message naming it, rather than a TypeError two frames away.

Internal fields

#[Internal] beside a field keeps it from anyone who may not edit the type: it is left out of what they read, and refused as a filter or a sort, so its value cannot be read back from which entries match. It suits, for example, an editor's note or a cost price.

Fields of your own

A field type is a subclass of Mainstay\Fields\Field, with no registration call: the attribute on the property is the instance. It names its column, its rules and its casts, and the Blade component that draws it.