how can disable the createAction If?

I need to block the Create User button if the logged in user does not have an associated business, I want the button to be visible but when clicked it does not redirect and shows a Filament Notification, I tried this code but it does not work for me, it does not go through the validation, I tried with dd() but it still doesn't happen, is there something I'm forgetting or missing? This is my code!
<?php
namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->before(function (Actions\CreateAction $action) {
// Validar si el usuario tiene negocios asociados
if ($this->shouldDisableCreate()) {
dd('hola');
Notification::make()
->warning()
->title('Acción no permitida')
->body('No puedes crear usuarios sin primero tener un negocio asociado.')
->persistent()
->actions([
Action::make('create')
->button()
->label('Crear usuario'),
])
->send();
// Detener la acción
$action->halt();
}
}),
];
}

private function shouldDisableCreate(): bool
{
return !auth()->user()->businesses()->exists();
}
}
<?php
namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->before(function (Actions\CreateAction $action) {
// Validar si el usuario tiene negocios asociados
if ($this->shouldDisableCreate()) {
dd('hola');
Notification::make()
->warning()
->title('Acción no permitida')
->body('No puedes crear usuarios sin primero tener un negocio asociado.')
->persistent()
->actions([
Action::make('create')
->button()
->label('Crear usuario'),
])
->send();
// Detener la acción
$action->halt();
}
}),
];
}

private function shouldDisableCreate(): bool
{
return !auth()->user()->businesses()->exists();
}
}
2 Replies
LeandroFerreira
protected function getHeaderActions(): array
{
$createAction = Actions\CreateAction::make();

if ($this->shouldDisableCreate()) {

$createAction = Actions\Action::make('create')
->label('New User')
->action(fn () => Notification::make()
->title('Disabled action')
->body('This action has been disabled.')
->warning()
->send()
);
}

return [
$createAction,
];
}
protected function getHeaderActions(): array
{
$createAction = Actions\CreateAction::make();

if ($this->shouldDisableCreate()) {

$createAction = Actions\Action::make('create')
->label('New User')
->action(fn () => Notification::make()
->title('Disabled action')
->body('This action has been disabled.')
->warning()
->send()
);
}

return [
$createAction,
];
}
TranceCode
TranceCodeOP4w ago
Thank you so much bro, the code it's working! Thank you :fi: 👏 👏 👏

Did you find this page helpful?