创建表单组件
Step 1:定义表单组件
例如,你需要创建用户管理的表单,那么应该在 app/Filament/Components 目录下创建 UserForm.php 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <?php
namespace App\Filament\Components;
use Filament\Forms\Components\{Select, TextInput};
class UserForm { public static function schema(): array { return [ TextInput::make('name') ->required() ->label('Name'), TextInput::make('email') ->email() ->required() ->label('Email Address'), Select::make('role') ->options([ 'admin' => 'Administrator', 'user' => 'User', ]) ->required() ->label('Role'), ]; } }
|
Step 2:使用组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?php
namespace App\Filament\Resources;
use App\Models\User; use Filament\Forms\Form; use Filament\Resources\Resource; use App\Filament\Components\UserForm; use App\Filament\Resources\UserResource\Pages;
class UserResource extends Resource { protected static ?string $model = User::class;
protected static ?string $navigationIcon = 'heroicon-o-user';
public static function form(Form $form): Form { return $form->schema( UserForm::schema(), ); }
public static function getPages(): array { return [ 'index' => Pages\ListUser::route('/'), 'create' => Pages\CreateUser::route('/create'), 'view' => Pages\ViewUser::route('/{record}'), 'edit' => Pages\EditUser::route('/{record}/edit'), ]; } }
|
通过调用 UserForm::schema(),你可以在项目的任何地方使用该表单。
适配不同的场景表单组件
更新表单组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?php
namespace App\Filament\Components;
use Filament\Forms\Components\{Select, TextInput};
class UserForm { public static function schema(array $options = []): array { return [ TextInput::make('name') ->required() ->hidden($options['nameHidden'] ?? false) ->label('Name'), TextInput::make('email') ->email() ->required() ->label('Email Address'), Select::make('role') ->options([ 'admin' => 'Administrator', 'user' => 'User', ]) ->required() ->label('Role'), ]; } }
|
Step 2:使用组件是传递参数
1 2 3 4 5 6 7 8 9 10 11
| <?php
public static function form(Form $form): Form { return $form->schema( UserForm::schema(options: [ 'nameHidden' => true, ]), ); }
|
参考:https://www.luckymedia.dev/blog/reusing-filament-forms