When developing with Filament, there are times when you might need to display success or failure notifications based on the results of an action. Using success
and failure
methods allows you to manage these notifications manually, giving more control over how and when messages are shown to users.
Step 1: Create the Base Action
Code
Let's start by creating a base Action
that will display a modal with specific input fields. This example sets up an action called "Regenerate Media Conversions," with a form where users can specify options for media conversion.
Action::make('Regenerate Media Conversions')
->requiresConfirmation()
->modalWidth('2xl')
->form([
Select::make('media')
->options([
null => 'All',
Product::class => 'Only Products',
Category::class => 'Only Categories',
]),
TextInput::make('ids')
->placeholder('1,2,3,4,5'),
Checkbox::make('only_missing')
->default(false),
Checkbox::make('with_responsive_images')
->default(true),
])
->action(function ($data) {
/* Perform your specific action here */
}),
This code creates a modal with a form containing fields for media selection, IDs, and additional options. With these inputs, you can then customize and perform any necessary tasks, such as regenerating media conversions.
Step 2: Set Success and Failure Titles
To make sure notifications appear upon success or failure, set the titles for these messages using the successNotificationTitle
and failureNotificationTitle
methods. Without these, no notifications will be shown even if the action succeeds or fails.
Action::make('Regenerate Media Conversions')
...
->successNotificationTitle('Process started. You will be notified.')
->failureNotificationTitle('Process failed.'),
Step 3: Modify the action
Method to Trigger Notifications
Now, let's update the action
method to actually throw the success
or failure
notifications. Here’s how to handle both cases within a try-catch block.
Action::make('Regenerate Media Conversions')
...
->action(function ($data, Action $action) {
try {
/* Perform your specific action */
$action->success(); // Trigger success notification
} catch (\Exception $e) {
$action->failure(); // Trigger failure notification
return false;
}
}),
Step 4: Customizing Success or Failure Messages
If you need to dynamically adjust the success or failure messages based on certain conditions, use the successNotificationTitle
and failureNotificationTitle
methods within the action
function. For instance:
Action::make('Regenerate Media Conversions')
->action(function ($data, Action $action) {
try {
/* Perform your specific action */
if ($isQueued) {
$action->successNotificationTitle('Process queued successfully.');
} else {
$action->successNotificationTitle('Process started immediately.');
}
$action->success(); // Trigger success notification
} catch (\Exception $e) {
$action->failureNotificationTitle('Process failed with error: ' . $e->getMessage());
$action->failure(); // Trigger failure notification
return false;
}
}),
This approach enables you to display custom messages for both queued and non-queued processes, as well as detailed error information if the action fails.
Conclusion
By manually setting success
and failure
notifications in Filament actions, you gain control over the feedback displayed to users, allowing for customized messages based on specific conditions or outcomes. This not only enhances user experience but also improves clarity when things go right or wrong.
Top comments (0)