Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions docs/05-panel-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,26 @@ public function panel(Panel $panel): Panel
}
```

You may also choose to hide a notification for a specific HTTP status code, such as `403`, by passing that status code to the `hiddenErrorNotification()` method. A hidden status code will still be caught by filament, but no notification will be shown.

Alternatively, you can use the `disabledErrorNotification()` method to fall back to Livewire's built-in error handling for that status code. This is useful if you want to hook into the Livewire error handling system to customize the error handling behavior for a specific status code but maintain Filament's error notification system for everything else.

```php
use Filament\Panel;

public function panel(Panel $panel): Panel
{
return $panel
// ...
->registerErrorNotification(
title: 'An error occurred',
body: 'Please try again later.',
)
->hiddenErrorNotification(403)
->disabledErrorNotification(503);
}
```

You can also enable or disable error notifications for specific pages in a panel by setting the `$hasErrorNotifications` property on the page class:

```php
Expand All @@ -497,9 +517,9 @@ use Filament\Pages\Dashboard as BaseDashboard;
class Dashboard extends BaseDashboard
{
protected ?bool $hasErrorNotifications = true;

// or

protected ?bool $hasErrorNotifications = false;

// ...
Expand Down Expand Up @@ -554,7 +574,7 @@ class Dashboard extends BaseDashboard
title: 'An error occurred',
body: 'Please try again later.',
);

$this->registerErrorNotification(
title: 'Record not found',
body: 'A record you are looking for does not exist.',
Expand All @@ -565,3 +585,28 @@ class Dashboard extends BaseDashboard
// ...
}
```

You may also choose to hide a notification for a specific HTTP status code, such as `403`, by passing that status code to the `hiddenErrorNotification()` method. A hidden status code will still be caught by filament, but no notification will be shown.

Alternatively, you can use the `disabledErrorNotification()` method to fall back to Livewire's built-in error handling for that status code. This is useful if you want to hook into the Livewire error handling system to customize the error handling behavior for a specific status code but maintain Filament's error notification system for everything else.

```php
use Filament\Pages\Dashboard as BaseDashboard;

class Dashboard extends BaseDashboard
{
protected function setUpErrorNotifications(): void
{
$this->registerErrorNotification(
title: 'An error occurred',
body: 'Please try again later.',
);

$this->hiddenErrorNotification(403);

$this->disabledErrorNotification(503);
}

// ...
}
```
2 changes: 1 addition & 1 deletion packages/panels/dist/index.js

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions packages/panels/resources/js/error-notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ document.addEventListener('livewire:init', () => {
}
}

preventDefault()

const errorNotification =
errorNotifications[status] ?? errorNotifications['']

if (errorNotification.isDisabled === true) {
return
}

preventDefault()

if (errorNotification.isHidden === true) {
return
}

new FilamentNotification()
.title(errorNotification.title)
.body(errorNotification.body)
Expand Down
2 changes: 1 addition & 1 deletion packages/panels/src/Facades/Filament.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* @method static string | null getEmailVerificationPromptUrl(array<string, mixed> $parameters = [])
* @method static string | null getSetUpRequiredMultiFactorAuthenticationUrl(array<string, mixed> $parameters = [])
* @method static string getEmailVerifiedMiddleware()
* @method static array<array{ title: string | Closure, body: string | Closure | null }> getErrorNotifications()
* @method static array<array{ title: ?string, body: ?string, isHidden: bool, isDisabled: bool }> getErrorNotifications()
* @method static string | null getFavicon()
* @method static string getFontFamily()
* @method static string getMonoFontFamily()
Expand Down
30 changes: 28 additions & 2 deletions packages/panels/src/Pages/Concerns/HasErrorNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait HasErrorNotifications
protected ?bool $hasErrorNotifications = null;

/**
* @var array<array{ title: string, body: ?string }>
* @var array<array{ title: ?string, body: ?string, isHidden: bool, isDisabled: bool }>
*/
protected array $errorNotifications = [];

Expand All @@ -20,6 +20,32 @@ public function registerErrorNotification(string $title, ?string $body = null, ?
$this->errorNotifications[$statusCode] = [
'title' => $title,
'body' => $body,
'isHidden' => false,
'isDisabled' => false,
];

return $this;
}

public function hiddenErrorNotification(int $statusCode): static
{
$this->errorNotifications[$statusCode] = [
'title' => null,
'body' => null,
'isHidden' => true,
'isDisabled' => false,
];

return $this;
}

public function disabledErrorNotification(int $statusCode): static
{
$this->errorNotifications[$statusCode] = [
'title' => null,
'body' => null,
'isHidden' => false,
'isDisabled' => true,
];

return $this;
Expand All @@ -31,7 +57,7 @@ public function hasErrorNotifications(): bool
}

/**
* @return array<array{ title: string, body: ?string }>
* @return array<array{ title: ?string, body: ?string, isHidden: bool, isDisabled: bool }>
*/
public function getErrorNotifications(): array
{
Expand Down
34 changes: 32 additions & 2 deletions packages/panels/src/Panel/Concerns/HasErrorNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait HasErrorNotifications
protected bool | Closure $hasErrorNotifications = true;

/**
* @var array<array{ title: string | Closure, body: string | Closure | null }>
* @var array<array{ title: string | Closure | null, body: string | Closure | null, isHidden: bool, isDisabled: bool }>
*/
protected array $errorNotifications = [];

Expand All @@ -30,27 +30,57 @@ public function registerErrorNotification(string | Closure $title, string | Clos
$this->errorNotifications[$statusCode] = [
'title' => $title,
'body' => $body,
'isHidden' => false,
'isDisabled' => false,
];

return $this;
}

public function hiddenErrorNotification(int $statusCode): static
{
$this->errorNotifications[$statusCode] = [
'title' => null,
'body' => null,
'isHidden' => true,
'isDisabled' => false,
];

return $this;
}

public function disabledErrorNotification(int $statusCode): static
{
$this->errorNotifications[$statusCode] = [
'title' => null,
'body' => null,
'isHidden' => false,
'isDisabled' => true,
];

return $this;
}

/**
* @return array<array{ title: string | Closure, body: string | Closure | null }>
* @return array<array{ title: ?string, body: ?string, isHidden: bool, isDisabled: bool }>
*/
public function getErrorNotifications(): array
{
$notifications = array_map(
fn (array $notification): array => [
'title' => $this->evaluate($notification['title']),
'body' => $this->evaluate($notification['body']),
'isHidden' => $notification['isHidden'],
'isDisabled' => $notification['isDisabled'],
],
$this->errorNotifications,
);

$notifications[''] ??= [
'title' => __('filament-panels::error-notifications.title'),
'body' => __('filament-panels::error-notifications.body'),
'isHidden' => false,
'isDisabled' => false,
];

return $notifications;
Expand Down
Loading