Can I pass in a filament resource's form into createOptionsForm()?

For example we have Post belongsToMany Tag and I want to be able to create new tags on-the-fly when selecting tags for a Post.
class TagResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->live(debounce: '1000')
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state)))
->required()
->unique()
->maxLength(50),
Forms\Components\TextInput::make('slug')
->required()
->unique()
->maxLength(50),
]);
}
class TagResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->live(debounce: '1000')
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state)))
->required()
->unique()
->maxLength(50),
Forms\Components\TextInput::make('slug')
->required()
->unique()
->maxLength(50),
]);
}
class PostResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('tags')
->multiple()
->relationship(titleAttribute: 'name')
->createOptionForm([]) // what is a DRY solution to avoid copy-pasting the form fields?
class PostResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('tags')
->multiple()
->relationship(titleAttribute: 'name')
->createOptionForm([]) // what is a DRY solution to avoid copy-pasting the form fields?
Solution:
You absolutely can do it!
Jump to solution
3 Replies
Solution
nicko170
nicko1705mo ago
You absolutely can do it!
nicko170
nicko1705mo ago
Forms\Components\Select::make('region_id') ->relationship('region', 'name') ->label('Region') ->required() ->createOptionForm(fn(Form $form) => RegionResource::form($form)) ->searchable()->preload(), Formatting on mobile sucks sorry!
@ryanvelbon
@ryanvelbon5mo ago
Yes, that did the trick! Nice and easy. Thank you. Here's the updated code:
Select::make('tags')
->multiple()
->relationship(titleAttribute: 'name')
->createOptionForm(fn(Form $form) => TagResource::form($form)),
Select::make('tags')
->multiple()
->relationship(titleAttribute: 'name')
->createOptionForm(fn(Form $form) => TagResource::form($form)),