Skip to content

Pattern input

The pattern input lets the user build a template out of free text and inline token chips — a document-numbering pattern is the typical case: literal text like RE- mixed with tokens like a sequential number, the year, or the month. Tokens are inserted from a menu, render as chips inline with the surrounding text, and — when a token declares its own config schema — are clickable to configure. Create one with PatternInput::make() and list the available tokens with ->tokens().

PatternInput::make('pattern', 'Number pattern')
->tokens([
PatternToken::make('NUMBER')
->label('Sequential number')
->configurable([
Choice::make('padding', 'Padding')->options([4 => '4', 5 => '5', 6 => '6'])->value(4),
]),
PatternToken::make('YYYY')->label('Year (4-digit)'),
PatternToken::make('MM')->label('Month'),
])
->requiredTokens(['NUMBER'])

The field submits an ordered array of segments — never a raw editor document. Each segment is either literal text or a placed token:

['pattern' => [
['type' => 'text', 'value' => 'RE-'],
['type' => 'token', 'token' => 'NUMBER', 'config' => ['padding' => '4']],
['type' => 'text', 'value' => '-'],
['type' => 'token', 'token' => 'YYYY', 'config' => []],
]]

Reading this array is application logic — the field itself has no opinion on how a pattern like this turns into an actual document number.

->tokens() takes the list of token types the pattern offers. Each is a PatternToken::make($name) where $name is the value stored on every placed segment of that kind. Give it a human label with ->label() (defaults to a title-cased version of the name) and, if it needs configuration, a schema of fields with ->configurable() — a normal array of fields, exactly like a Builder row’s schema:

PatternToken::make('NUMBER')
->label('Sequential number')
->configurable([
Choice::make('padding', 'Padding')->options([4 => '4', 5 => '5', 6 => '6'])->value(4),
]);

A token without ->configurable() (like YYYY and MM above) renders as a plain chip with nothing to click. Each token type can only be placed once per pattern — the insert menu hides a token once it’s already in use.

->requiredTokens() lists token names that must appear somewhere in the pattern for it to be valid:

PatternInput::make('pattern')
->tokens([PatternToken::make('NUMBER'), PatternToken::make('YYYY')])
->requiredTokens(['NUMBER']);

A submitted pattern missing a required token fails validation, alongside an unknown token name or the same token placed twice.

->separator() sets the text inserted between a newly-placed chip and whatever’s already there when a token is added from the menu — a convenience for the common case of dash- or slash-separated patterns, not a stored or validated part of the value:

PatternInput::make('pattern')->separator('-');

PatternInput shares label, required, disabled, read-only, and visibility options with every field — see Fields. For validation and conditional behavior, see Validation and Conditional fields.