在继续之前,请确保 filament/forms 已安装在您的项目中。您可以通过运行以下命令进行检查:
composer show filament/forms如果尚未安装,请查阅安装指南并根据说明配置各个组件。
首先,生成一个新的 Livewire 组件:
php artisan make:livewire CreatePost然后,在页面上渲染你的 Livewire 组件:
@livewire('create-post')或者,你可以使用一个全页的 Livewire 组件:
use App\Livewire\CreatePost;
use Illuminate\Support\Facades\Route;
Route::get('posts/create', CreatePost::class);在继续之前,请确保您的项目中已安装 Forms 包。请查阅 安装指南 并根据说明配置 各个组件。
在将表单添加到Livewire组件类时,有5个主要任务。每个都至关重要:
HasSchemas 接口并使用 InteractsWithSchemas trait。$data,但你可以随意命名。form() 方法,在这里配置表单。添加表单的 schema,并告诉 Filament 将表单数据存储在 $data 属性中 (使用 statePath('data'))。mount() 中使用 $this->form->fill() 初始化表单。这对于你构建的每个表单都是必不可少的,即使它没有任何初始数据。create(),但你可以随意命名。在该方法内部,你可以使用 $this->form->getState() 验证并获取表单数据。重要的是,你应当使用此方法,而不是直接访问 $this->data 属性,因为表单数据在返回之前需要经过验证并转换为有用格式。<?php
namespace App\Livewire;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Illuminate\Contracts\View\View;
use Filament\Schemas\Schema;
use Livewire\Component;
class CreatePost extends Component implements HasSchemas
{
use InteractsWithSchemas;
public ?array $data = [];
public function mount(): void
{
$this->form->fill();
}
public function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('title')
->required(),
MarkdownEditor::make('content'),
// ...
])
->statePath('data');
}
public function create(): void
{
dd($this->form->getState());
}
public function render(): View
{
return view('livewire.create-post');
}
}最后,在你的 Livewire 组件视图中,渲染表单:
<div>
<form wire:submit="create">
{{ $this->form }}
<button type="submit">
Submit
</button>
</form>
<x-filament-actions::modals />
</div><x-filament-actions::modals /> 用于渲染表单组件 [操作模态框](../schemas/actions) 。该代码可以放置在 <form> 元素之外的任何位置,只要它在 Livewire 组件内部即可。
在浏览器中访问你的 Livewire 组件,你应该会看到 components() 中的表单组件:
提交带有数据的表单,你将看到表单数据被转储到屏幕上。你可以将数据保存到一个模型中,而不是转储它:
use App\Models\Post;
public function create(): void
{
Post::create($this->form->getState());
}filament/forms 也包含以下包:
filament/模式这些包允许你在 Livewire 组件中使用它们的组件。
例如,如果你的表单使用了 操作,请记住在你的 Livewire 组件类上实现 HasActions 接口并使用 InteractsWithActions trait。
如果您正在使用任何其他 Filament 组件 在您的表单中,请务必安装并集成相应的包。
要使用数据填充表单,只需将该数据传递给 $this->form->fill() 方法。
例如,如果你正在编辑一个现有的帖子,你可能会这样做:
use App\Models\Post;
public function mount(Post $post): void
{
$this->form->fill($post->attributesToArray());
}务必使用 $this->form->fill() 方法而不是直接将数据赋值给 $this->data 属性。这是因为提交的数据需要在存储之前内部转换为有用的格式。
让 $form 访问模型很有用,原因有几点:
$this->form->fill([...])时,Filament 将自动加载该帖子的评论,并在你调用$this->form->getState()时,将其保存回关联。exists() 和 unique() 的验证规则可以自动从模型中检索数据库表名。建议在有模型时,始终将模型传递给表单. 如前所述,它解锁了 Filament 表单系统的许多新功能.
要将模型传递给表单,请使用$form->model()方法:
use Filament\Schemas\Schema;
public Post $post;
public function form(Schema $schema): Schema
{
return $schema
->components([
// ...
])
->statePath('data')
->model($this->post);
}In some cases, the form's model is not available until the form has been submitted. For example, in a Create Post form, the post does not exist until the form has been submitted. Therefore, you can't pass it in to $form->model(). However, you can pass a model class instead:
use App\Models\Post;
use Filament\Schemas\Schema;
public function form(Schema $schema): Schema
{
return $schema
->components([
// ...
])
->statePath('data')
->model(Post::class);
}单独来看,这不如传递模型实例强大。例如,文章创建后,关系将不会被保存到文章中。为此,你需要在文章创建后将它传递给表单,并调用 saveRelationships() 将关系保存到其中:
use App\Models\Post;
public function create(): void
{
$post = Post::create($this->form->getState());
// Save the relationships from the form to the post after it is created.
$this->form->model($post)->saveRelationships();
}在我们之前的所有示例中,我们一直将表单数据保存到 Livewire 组件上的公共 $data 属性。然而,您可以将数据保存到独立的属性中。例如,如果您有一个带有 title 字段的表单,您可以将表单数据保存到 $title 属性中。为此,完全不要向表单传递 statePath()。确保您的所有字段在类上都具有自己的 公共 属性。
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
public ?string $title = null;
public ?string $content = null;
public function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('title')
->required(),
MarkdownEditor::make('content'),
// ...
]);
}许多表单可以使用 InteractsWithSchemas 特性定义。每个表单都应该使用一个同名方法:
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
public ?array $postData = [];
public ?array $commentData = [];
public function editPostForm(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('title')
->required(),
MarkdownEditor::make('content'),
// ...
])
->statePath('postData')
->model($this->post);
}
public function createCommentForm(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('name')
->required(),
TextInput::make('email')
->email()
->required(),
MarkdownEditor::make('content')
->required(),
// ...
])
->statePath('commentData')
->model(Comment::class);
}现在,每个表单都可以通过其名称进行寻址,而不是通过 form。例如,要填写帖子表单,你可以使用 $this->editPostForm->fill([...]),或者要从评论表单获取数据,你可以使用 $this->createCommentForm->getState()。
你会注意到每个表单都有其独特的 statePath()。每个表单都会将其状态写入你的 Livewire 组件上的一个不同数组,因此,定义公有属性, $postData 和 $commentData 在本例中很重要。
您可以随时通过调用 $this->form->fill() 将表单重置为其默认数据。 例如,您可能希望在每次提交表单后清除其内容:
use App\Models\Comment;
public function createComment(): void
{
Comment::create($this->form->getState());
// Reinitialize the form to clear its data.
$this->form->fill();
}建议您学习如何手动设置带有表单的 Livewire 组件,但一旦您熟练掌握,就可以使用 CLI 为您生成表单。
php artisan make:filament-livewire-form RegistrationForm这将生成一个新的 app/Livewire/RegistrationForm.php 组件,你可以对其进行自定义。
Filament 也能够为特定的 Eloquent 模型生成表单。这些更强大,因为它们会自动为您保存表单中的数据,并且确保表单字段已正确配置以访问该模型。
当使用 make:livewire-form 命令生成表单时,它将询问模型的名称:
php artisan make:filament-livewire-form Products/CreateProduct默认情况下,将模型传递给 make:livewire-form 命令将生成一个在数据库中创建新记录的表单。 如果您将 --edit 标志传递给该命令,它将为特定记录生成一个编辑表单。 这将自动用记录中的数据填充表单,并 将数据保存回模型 当表单提交时。
php artisan make:filament-livewire-form Products/EditProduct --editFilament 还能够根据模型的数据列猜测你希望在模式中包含哪些表单字段。你可以在生成表单时使用 --generate 标志:
php artisan make:filament-livewire-form Products/CreateProduct --generate