Actions - 预制 Action
Edit action
概述
Filament 包含一个可以编辑 Eloquent 记录的预制 Action。点击触发按钮后,会打开一个带有表单的模态框。用户填写表单,数据经过验证后存入到数据库。你可以这样使用:
use Filament\Actions\EditAction;use Filament\Forms\Components\TextInput; EditAction::make()    ->record($this->post)    ->form([        TextInput::make('title')            ->required()            ->maxLength(255),        // ...    ])如果你想要编辑表格行记录,你可以使用 Filament\Tables\Actions\EditAction:
use Filament\Forms\Components\TextInput;use Filament\Tables\Actions\EditAction;use Filament\Tables\Table; public function table(Table $table): Table{    return $table        ->actions([            EditAction::make()                ->form([                    TextInput::make('title')                        ->required()                        ->maxLength(255),                    // ...                ]),        ]);}表单填充前自定义数据
如果你想要在数据填充到表单前,对数据进行修改;你可以使用 mutateRecordDataUsing() 方法,修改 $data 数组,并且返回其填充到表单前修改的版本:
EditAction::make()    ->mutateRecordDataUsing(function (array $data): array {        $data['user_id'] = auth()->id();         return $data;    })保存前自定义数据
有时候,你可能希望在表单数据存入数据库之前对其进行修改。为此你可以使用 mutateFormDataUsing() 方法,该方法将 $data 当作数组访问,并返回修改后的版本:
EditAction::make()    ->mutateFormDataUsing(function (array $data): array {        $data['last_edited_by_id'] = auth()->id();         return $data;    })自定义保存过程
你可以使用 using() 方法微调记录的更新方式:
use Illuminate\Database\Eloquent\Model; EditAction::make()    ->using(function (Model $record, array $data): Model {        $record->update($data);         return $record;    })保存后重定向
使用 successRedirectUrl() 方法中,你可以对表单提交后的重定向进行自定义设置:
EditAction::make()    ->successRedirectUrl(route('posts.list'))如果你想要使用更新后的记录进行重定向,请使用 $record 参数:
use Illuminate\Database\Eloquent\Model; EditAction::make()    ->successRedirectUrl(fn (Model $record): string => route('posts.view', [        'post' => $record,    ]))自定义保存通知
记录更新成功后,会发送一个通知给用户,说明操作成功:
要自定义通知标题,请使用 successNotificationTitle() 方法:
EditAction::make()    ->successNotificationTitle('User updated')使用 successNotification() 方法,你可以自定义整个通知:
use Filament\Notifications\Notification; EditAction::make()    ->successNotification(       Notification::make()            ->success()            ->title('User updated')            ->body('The user has been saved successfully.'),    )要完全禁用通知,请使用 successNotification(null) 方法:
EditAction::make()    ->successNotification(null)生命周期钩子
钩子可用在 Action 生命周期的各种节点中执行代码,比如表单数据保存之前。
可用的钩子有:
EditAction::make()    ->beforeFormFilled(function () {        // Runs before the form fields are populated from the database.    })    ->afterFormFilled(function () {        // Runs after the form fields are populated from the database.    })    ->beforeFormValidated(function () {        // Runs before the form fields are validated when the form is saved.    })    ->afterFormValidated(function () {        // Runs after the form fields are validated when the form is saved.    })    ->before(function () {        // Runs before the form fields are saved to the database.    })    ->after(function () {        // Runs after the form fields are saved to the database.    })中断保存处理过程
在任何时候,你都可以在生命周期钩子或者 mutation 方法中,调用 $action->halt(),中断整个保存处理过程:
use App\Models\Post;use Filament\Notifications\Actions\Action;use Filament\Notifications\Notification;use Filament\Tables\Actions\EditAction; EditAction::make()    ->before(function (EditAction $action, Post $record) {        if (! $record->team->subscribed()) {            Notification::make()                ->warning()                ->title('You don\'t have an active subscription!')                ->body('Choose a plan to continue.')                ->persistent()                ->actions([                    Action::make('subscribe')                        ->button()                        ->url(route('subscribe'), shouldOpenInNewTab: true),                ])                ->send();                    $action->halt();        }    })如果你想同时关闭 Action 模态框,你可以使用 cancel() 完全取消该 Action:
$action->cancel();Still need help? Join our Discord community or open a GitHub discussion