Where would you put action logic that handles validation?

Refactoring some duplicate code and wondering where you recommend putting validation logic for deletes on both DeleteAction and DeleteBulkAction. E.g
Tables\Actions\DeleteBulkAction::make()
->before(function (Tables\Actions\DeleteBulkAction $action) {
foreach ($action->getRecords() as $record) {

if ($record->staff()->exists()) {
Notification::make('not_deletable')
->warning()
->title('This record can not be deleted because it is in use')
->body(__('Reason why'))
->persistent()
->actions([
Action::make('staff')
->button()
->url(StaffResource::getUrl(
'index',
['tableFilters[project_id][value]' => $record->id]
)),
])
->send();

$action->cancel();

}
Tables\Actions\DeleteBulkAction::make()
->before(function (Tables\Actions\DeleteBulkAction $action) {
foreach ($action->getRecords() as $record) {

if ($record->staff()->exists()) {
Notification::make('not_deletable')
->warning()
->title('This record can not be deleted because it is in use')
->body(__('Reason why'))
->persistent()
->actions([
Action::make('staff')
->button()
->url(StaffResource::getUrl(
'index',
['tableFilters[project_id][value]' => $record->id]
)),
])
->send();

$action->cancel();

}
Filament\Actions\DeleteAction::make()
->before(function (Filament\Actions\DeleteAction $action) {
$record = $action->getRecord();

if ($record->staff()->exists()) {
Notification::make('not_deletable')
->warning()
->title('This record can not be deleted because it is in use')
->body(__('Reason why'))
->persistent()
->actions([
Action::make('staff')
->button()
->url(StaffResource::getUrl(
'index',
['tableFilters[project_id][value]' => $record->id]
)),
])
->send();

$action->cancel();

}
Filament\Actions\DeleteAction::make()
->before(function (Filament\Actions\DeleteAction $action) {
$record = $action->getRecord();

if ($record->staff()->exists()) {
Notification::make('not_deletable')
->warning()
->title('This record can not be deleted because it is in use')
->body(__('Reason why'))
->persistent()
->actions([
Action::make('staff')
->button()
->url(StaffResource::getUrl(
'index',
['tableFilters[project_id][value]' => $record->id]
)),
])
->send();

$action->cancel();

}
5 Replies
krekas
krekas16mo ago
Trait maybe
exactwebitesolutions
Thanks what would you call it and store it in what namespace stucture?
krekas
krekas16mo ago
Calling is a personal preference as well as where to store
exactwebitesolutions
The reason I asked was to learn what other people are doing. Maybe there's a structural pattern to follow.
krekas
krekas16mo ago
For traits I would say personal preference
Want results from more Discord servers?
Add your server