FilamentF
Filament3y ago
Sesh

How to stop an action on certain conditions with ->before()

What is the best practice in Filament to stop an action from continuing before it runs into a exception? For example I want to show a message to the user if delete is not possible because the category is used for items:

                    DeleteAction::make('delete')
                        ->before(function (TrendRadarTrendSegment $record) {
                            if ($record->radarTrends()->count() > 0) {
                                Notification::make()
                                    ->title('Delete failed')
                                    ->body(__('This segment is used by trends and cannot be deleted. Please move the trends to another segment first.'))
                                    ->danger()
                                    ->send();
                            }
                        })

How can I break out of the DeleteAction in this case? Or is there another way to catch en exception and show a notification?
Was this page helpful?