Filament:给表单添加自定义按钮-重置示例以清除字段的值

在Filament表单中,有“创建”和“取消”按钮。如果您想添加另一个操作,如“重置”以清除所有/某些字段,该怎么办?让我们看看怎么做。

添加操作按钮

首先,让我们添加Action本身。因为这是一个创建页面,我们需要将其添加到create类中。
App\Filament\Resources\ArticleResource\Pages/CreateArticle.php:

...
use Filament\Actions\Action;
use Filament\Notifications\Notification;
...

    protected function getFormActions(): array
    {
        return array_merge(parent::getFormActions(), [
            Actions\Action::make('重置')
                ->action(function () {
                    $this->fillForm(); //or $this->reset();
                    Notification::make()
                        ->title('表单已重置')
                        ->success()
                        ->send();
                })
        ],
        );
    }
...

同样,在编辑类上也加上这个方法:
App\Filament\Resources\ArticleResource\Pages/EditArticle.php:

protected function getFormActions(): array
{
    return array_merge(parent::getFormActions(), [
        Actions\Action::make('重置')
            ->action(function () {
                $this->fillForm();
                Notification::make()
                    ->title('表单已重置')
                    ->success()
                    ->send();
            })
    ],
    );
}

本作品采用《CC 协议》,转载必须注明作者和本文链接
人生就是马拉松,精彩的是后半程
running8
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

这个开发速度怎么样?

6个月前 评论
running8 (楼主) 6个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!