Form types
A form's type decides which fields it has in code, whether editors may add
their own on the canvas, and what happens around a submission. The types
config maps the name a form stores in its template column to the class behind
it:
'types' => [
'custom' => Forms\CustomForm::class,
'contact' => Forms\ContactForm::class,
'application' => App\Forms\VacancyApplication::class,
],custom leaves every field to the editor, contact has all of them in code.
A new form is custom; editors pick another type behind the "Use a fixed form"
switch, which only shows when custom and at least one other type are
registered. A type of your own extends FormType and returns its fields from fields(),
built with the same field classes the canvas stores:
use VanOns\FilamentFormBuilder\Enums\FieldWidth;
use VanOns\FilamentFormBuilder\Filament\FormBuilder\Fields;
use VanOns\FilamentFormBuilder\Forms\CustomFields;
use VanOns\FilamentFormBuilder\Forms\FormType;
class VacancyApplication extends FormType
{
public static function getLabel(): string
{
return 'Sollicitatie';
}
public function fields(): array
{
return [
Fields\TextInputField::make('voornaam')->label('Voornaam')->required()->span(FieldWidth::HALF),
Fields\TextInputField::make('achternaam')->label('Achternaam')->required()->span(FieldWidth::HALF),
Fields\TextInputField::make('vacature')->hidden()->default(request()->query('vacature')),
CustomFields::make(),
Fields\CheckboxField::make('privacy')->label('Ik ga akkoord met de privacyverklaring')->required(),
Fields\SubmitField::make('verstuur')->label('Verstuur'),
];
}
}CustomFields::make()marks where the fields an editor builds go. A type without it has no canvas.- The canvas shows the type's own fields around the editor's, locked in place,
and marks the editor's part as "Custom fields". That part always starts and ends on a
row of its own, on the page too. The type's keys stay reserved, and so do
those of its
extraValues(), so an editor's field never takes one. - A field added in code later with a key an editor's field already has wins: the editor's field is left out of the form, with a warning in the log, and the canvas flags it until it is deleted or gets another key.
- A hidden field renders as
<input type="hidden">holding its default value, which suits context such as the vacancy a visitor applies for. - Any field with a default can start with a parameter of the page's URL
instead: "Fill from the URL" under Advanced on the canvas, or
->defaultFromQuery('vacature')in code, so?vacature=Adviseurfills it. Without the parameter the default applies. A visitor can change the URL, so it suits context, not something to trust. - A type without fields of its own and without
CustomFields::make()shows no fields section at all; one with fields but no marker shows them with only their texts to change.
Texts an editor may change
An editor may change the texts of a field from code: a click on it opens a slide-over with just those. Its key, type, rules, required, width, place and conditions stay with the code, and so do the values of its options.
| Field type | Texts |
|---|---|
| Text, email, phone, number, text area, dropdown | label, description, placeholder |
| Date, upload, checkbox | label, description |
| Radio, checkbox list, dropdown | also the labels of the options |
| Consent | its text, label, description |
| Title, step | title |
| Text block | text |
| Submit | label |
The form stores only what differs from the code, in custom.overrides by field
key, so a text the developer changes later still comes through where the editor
left it alone; emptying a text, or Back to default, brings the code's back.
An option's label goes by its value, so it stays with its option when the code
adds or moves one, and the value is what a submission stores. Everything that
reads the form's fields follows: the page, the validation messages, the merge
tags, the export and the submission's page.
editable() narrows it per field, or opens up a setting of the field type
beyond its texts:
Fields\ConsentField::make('voorwaarden')->editable(false), // nothing
Fields\EmailField::make('email')->label('Email')->editable(['label']), // only the label
Fields\RadioField::make('soort')->editable(['label', 'description']), // not the option labelsA field type of your own lists its texts in editableSettings().
Hooks
Every method below runs on an instance, with the form in $this->form.
| Method | When it runs | Affects |
|---|---|---|
beforeValidation(array $data) | Before validation | What is validated, and so what is stored |
beforeStore(array $data) | Before the submission is stored | Stored data |
formatValues(array $values, FormSubmission $submission) | Whenever answers are shown | The table, the detail page, the export, the mails and their placeholders |
afterSubmission(FormSubmission $submission) | Once a visitor's submission is stored | Queues the notifications and the integrations |
response(FormSubmission $submission) | After that | Replaces the redirect or message when it returns something |
A visitor can only post the keys of the form's fields. A value beforeStore()
adds on its own is declared in extraValues() as key => label, so it gets a
label, a placeholder and a column like any field:
public function extraValues(): array
{
return ['received_from' => 'Received from'];
}
public function beforeStore(array $data): array
{
return [...$data, 'received_from' => request()->headers->get('referer')];
}formatValues() receives the answers after the fields formatted them (a choice
already reads as its label, a file as its download link) and keeps the field
keys. Whatever it returns is what every output shows:
public function formatValues(array $values, FormSubmission $submission): array
{
return [...$values, 'property_type' => PropertyType::tryFrom($values['property_type'] ?? '')?->getLabel()];
}rules() gives rules by field key, added after the rules of the field itself.
A field in code takes its own with ->rules(); this is for a field an editor
built on the canvas, and for a rule about more than one field:
public function rules(): array
{
return [
'email' => ['unique:users,email'],
'einddatum' => ['after:startdatum'],
];
}They are checked when the form is sent and, where the server checks each step, with the step their field is in. A rule for a key the form has no field for is left out, so an editor who removes that field does not lock the form.
A type may also override settings() (extra fields in the form's settings,
under the canvas, stored in its settings column), messages() (validation
messages), and the admin toggles described below.
Settings every form should have, whatever its type, go on the plugin. They show
next to the type's own and are stored in the same settings column; the
closure gets the type picked, so a setting can also be for one type only:
FilamentFormBuilderPlugin::make()
->formSettings(fn (?FormType $type): array => [
Toggle::make('show_in_footer')->label('Show in the footer'),
...$type instanceof VacancyForm ? [TextInput::make('department')->label('Department')] : [],
]);Read them as $form->settings['show_in_footer'].
