Do not run BulkAction for any records if some do not pass policy validation

Hey Filament Discord! First of all, huge thanks for all maintainers of this package. I have a question regarding bulk actions. I saw that it is possible to individually authorize them for each record, which is cool:
BulkAction::make('do_some_stuff')
->authorizeIndividualRecords('doStuff')
->action(self::executeAction(...))
BulkAction::make('do_some_stuff')
->authorizeIndividualRecords('doStuff')
->action(self::executeAction(...))
In this case, executeAction will only receive the records which pass doStuff, and will only execute my action for those. However, in such cases where some records get thrown out, I would like if the action did not execute for any records, not even for those that passed the policy. I saw that I can inject the $records and $action in my action() callback, and can look for differences if needed:
use Filament\Actions\Action;

private static function executeAction(Collection $records Action $action): void
{
$originallySelectedRecords = $action->getSelectedRecords();

if ($records->count() < $originallySelectedRecords->count()) {
// Uh oh, some did not pass the policy. I would not like to run this at all, not even for those that did.
return;
}
}
use Filament\Actions\Action;

private static function executeAction(Collection $records Action $action): void
{
$originallySelectedRecords = $action->getSelectedRecords();

if ($records->count() < $originallySelectedRecords->count()) {
// Uh oh, some did not pass the policy. I would not like to run this at all, not even for those that did.
return;
}
}
But it'd be a bit troublesome to insert this logic into all of my bulk actions. So my question is, is there a more elegant, Filament way I can do this? Refusing to run the action when some records do not pass a policy? Or would I need to extend BulkAction and add some custom logic? If so, could I get some tips on how I can cleanly do it? My reason for wanting to do this is that I think it's simpler to understand for the end user. "Your selection mixed in some incorrect records, so nothing will be done, please try again." is the way I want to go by. Thank you for your help in advance.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?