重复使用资源中的表单以在Forms\Components\Select(FilamentPhp)中创建记录。

huangapple go评论57阅读模式
英文:

Reuse a form from a resource to create a record in Forms\Components\Select (FilamentPhp)

问题

我正在使用FilamentPhp创建一个仪表板。我有一个用于作者的资源和一个用于由这些作者创建的元素的资源。在元素资源中,我有以下代码:

Forms\Components\Select::make('author_id')->relationship('author', 'name')

现在我希望能够在不离开元素创建/编辑表单的情况下创建作者。我发现这个组件有一个createOptionForm方法,可以实现我想要的功能,但它需要一个新的表单架构。有没有办法调用已经存在于作者资源中的表单架构?

我尝试过向该函数发送资源、操作和URL,但我无法使其正常工作,而不复制整个表单。我不喜欢这个解决方案,因为它会增加作者部分的维护时间,并且我还有一些类似的情况。

英文:

I'm doing a dashboard using FilamentPhp. I have a resource for authors and a resource for elements created by those authors. In the elements resource I have:

Forms\Components\Select::make('author_id')->relationship('author', 'name')

Now I want to be able to create an author without leaving the element create/edit form. I found that the component has a createOptionForm method that allows to do what I want but it recives a new form schema. Are there any way to call the one already existing in the authors resource?

I have tried sending this funcion the resource, the action, the url, but I can't get it to work withou copying the whole form. I don't like this solution because it doubles the maintenance time of the authors section and I have some more cases like that.

答案1

得分: 0

你可以这样做:

$authorForm = new Form;

$authorForm = AuthorResource::form($authorForm);

return $form
    ->schema([

        Forms\Components\Select::make('author_id')
            ->label('Author')
            ->relationship('author', 'name')
            ->createOptionForm($authorForm->getSchema()),
    ]);

这样,首先你从你的资源中实例化一个表单,然后在你的选择器中使用该资源的模式通过 getSchema 方法。

编辑:

在 Filament V3.0 中不再起作用。我找到的新解决方案是:

创建一个名为 AuthorForm 的控制器。在其中创建一个名为 "getForm()" 的静态方法。它应该返回类似这样的表单数组:

class AuthorForm extends Controller
{
    static function getForm() : array {
        return [
            Forms\Components\TextInput::make("name"),
            Forms\Components\TextInput::make("last_name"),
            /*...*/
        ];
    }
}

然后,每当你需要表单时,只需调用该方法:

return $form
    ->schema([

        Forms\Components\Select::make('author_id')
            ->label('Author')
            ->relationship('author', 'name')
            ->createOptionForm(AuthorForm::getForm()),
    ]);

这也适用于重复器或关系管理器中。

当然,你也应该在主资源中使用它,这样你只需要在一个地方维护表单:

class AuthorResource extends Resource
{

    public static function form(Form $form): Form
    {
        return $form
            ->schema(AuthorForm::getForm());
    }

    /*...*/
}
英文:

You can do it this way:

    $authorForm = new Form;

    $authorForm = AuthorResource::form($authorForm);

    return $form
        ->schema([

            Forms\Components\Select::make('author_id')
                ->label('Author')
                ->relationship('author', 'name')
                ->createOptionForm($authorForm->getSchema()),
        ]);

This way you first instanciate a form from your resource, then on your select use the schema for this resource by 'getSchema' method.

Edit:

No longer working on Filament V3.0. New walkaround I found is:

Create a controller AuthorForm. On it, create a "getForm()" static method. It should return form array like this:

        class AuthorForm extends Controller
        {
                static function getForm() : array {
                        return [
                                Forms\Components\TextInput::make("name"),
                                Forms\Components\TextInput::make("last_name"),
                                /*...*/
                        ];
                }
        }

Then, just call that method whenever you need the form:


        return $form
        ->schema([

            Forms\Components\Select::make('author_id')
                ->label('Author')
                ->relationship('author', 'name')
                ->createOptionForm(AuthorForm::getForm()),
        ]);

This works also for repeaters or in relationship managers.

Of course, you should use it on the main resource as well, so you only need to do maintenance of form in one place:


class AuthorResource extends Resource
{

    public static function form(Form $form): Form
    {
        return $form
            ->schema(AuthorForm::getForm());
    }

    /*...*/
}

huangapple
  • 本文由 发表于 2023年7月23日 16:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定