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 withrequired: true, and a#[Relation]or#[Terms]that holds a list is required only withrequired: true, since an empty list is a list holding none.localized:gives the field a value per language, kept in the type's_localestable.label:is what the admin calls it, when the property's name does not say it well.
The field types
An example
#[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.