Il Power del Form Builder
Filament offre un Form Builder incredibilmente flessibile per creare interfacce complesse senza scrivere HTML.
Form Layout Avanzati
Organizza i campi con layout professionali:
<?php
use Filament\Forms\Components;
public static function form(Form $form): Form
{
return $form->schema([
Components\Section::make('Informazioni Principali')
->description('Inserisci i dati base del prodotto')
->schema([
Components\Grid::make(2)
->schema([
Components\TextInput::make('name')
->required()
->maxLength(255),
Components\TextInput::make('sku')
->unique(ignoreRecord: true)
->required(),
]),
Components\RichEditor::make('description')
->columnSpanFull(),
]),
Components\Section::make('Pricing')
->columns(3)
->schema([
Components\TextInput::make('price')
->numeric()
->prefix('€')
->required(),
Components\TextInput::make('cost')
->numeric()
->prefix('€'),
Components\TextInput::make('margin')
->numeric()
->suffix('%')
->disabled()
->dehydrated(false),
]),
Components\Tabs::make('Additional Info')
->tabs([
Components\Tabs\Tab::make('SEO')
->schema([
Components\TextInput::make('meta_title'),
Components\Textarea::make('meta_description')
->rows(3),
]),
Components\Tabs\Tab::make('Images')
->schema([
Components\FileUpload::make('images')
->multiple()
->image()
->maxFiles(5)
->reorderable(),
]),
]),
]);
}
?>
Validazione Custom
Aggiungi regole di validazione personalizzate:
<?php
Components\TextInput::make('email')
->email()
->required()
->rules([
function () {
return function (string $attribute, $value, \Closure $fail) {
if (User::where('email', $value)->exists()) {
$fail('Questa email è già in uso.');
}
};
},
]),
// Validazione con regex
Components\TextInput::make('phone')
->tel()
->regex('/^[+]?[0-9]{10,}$/')
->validationMessages([
'regex' => 'Il numero di telefono non è valido.',
]),
?>
Campi Dipendenti
Campi che reagiscono ad altri campi:
<?php
Components\Select::make('country')
->options([
'it' => 'Italia',
'us' => 'USA',
'uk' => 'UK',
])
->live()
->required(),
Components\Select::make('state')
->options(function (callable $get) {
$country = $get('country');
if (!$country) {
return [];
}
return match($country) {
'it' => Region::where('country', 'it')->pluck('name', 'id'),
'us' => State::where('country', 'us')->pluck('name', 'id'),
default => [],
};
})
->required(),
?>
I campi live si aggiornano automaticamente quando cambiano le loro dipendenze!