Reuse form componenets

How can you reuse a section of components in several resources without having to paste the same code in all the resources?
Solution:
You can create separate class with static method that define form fields and then call that method in multiple resources, like this: ```php class MyFormFields {...
Jump to solution
1 Reply
Solution
tjodalv
tjodalv5mo ago
You can create separate class with static method that define form fields and then call that method in multiple resources, like this:
class MyFormFields
{
public static function form(): array
{
return [
Filament\Forms\Components\TextInput::make('name'),
...
];
}
}
class MyFormFields
{
public static function form(): array
{
return [
Filament\Forms\Components\TextInput::make('name'),
...
];
}
}
And then in your resource class:
class YourResourceClass
{
public static function form(Form $form): Form
{
return $form
->schema(...MyFormFields::form());
}
}
class YourResourceClass
{
public static function form(Form $form): Form
{
return $form
->schema(...MyFormFields::form());
}
}