Skip to content

Alerts

The AlertManagerInterface provides a structured and expressive way to trigger SweetAlert2 modal alerts within your Symfony application.

Overview

Alerts are modal dialogs used to notify users or require confirmation. Unlike toasts, alerts are blocking interactions and typically require user action (e.g., confirmation or cancellation).

This library stores alerts in the session under an internal storage key and renders them on the next page load. Flash messages are only converted when the bundle is configured to auto-convert them.

Usage

Inject AlertManagerInterface and use the helper methods to create alerts:

use Pentiminax\UX\SweetAlert\AlertManagerInterface;
public function someAction(AlertManagerInterface $alertManager): Response
{
$alertManager->success(
title: 'Update Successful',
text: 'Your settings have been saved.'
);
return $this->redirectToRoute('dashboard');
}

Alert types

The following predefined alert types are available:

  • success()
  • error()
  • warning()
  • info()
  • question()
  • input()

Each method returns an Alert object that can be further customized using a fluent API.

Parameters

NameTypeDefaultDescription
idstring(required)Unique identifier
titlestring(required)Alert title
textstring''Description text
positionPosition enumPosition::CENTERModal position on screen

Customization

After creating an alert, you can customize its behavior:

$alert = $this->alertManager->info(
title: 'Info Alert',
text: 'This is an info alert',
);
$alert
->withCancelButton()
->withDenyButton();

Available customization methods

MethodDescription
withCancelButton()Shows a cancel button
withDenyButton()Shows a deny button
withoutConfirmButton()Hides the confirm button
withoutAnimation()Disables animation
withoutBackdrop()Removes modal backdrop
theme(Theme $theme)Sets the theme (default auto)
confirmButtonColor(string)Sets confirm button color (hex value)
cancelButtonColor(string)Sets cancel button color (hex value)
denyButtonColor(string)Sets deny button color (hex value)
reverseButtons(bool)Reverses the order of the buttons
denyOutsideClick()Prevents closing the modal by clicking outside
denyEscapeKey()Prevents closing the modal with the ESC key

Input dialogs

Use input() when you want to collect user data through a SweetAlert2 modal from PHP:

use Pentiminax\UX\SweetAlert\AlertManagerInterface;
use Pentiminax\UX\SweetAlert\InputType\Select;
public function invite(AlertManagerInterface $alertManager): Response
{
$alertManager->input(
inputType: new Select(
options: [
'admin' => 'Admin',
'editor' => 'Editor',
],
label: 'Choose a role',
value: 'editor',
),
title: 'Invite user',
text: 'Select the role to apply.',
);
return $this->redirectToRoute('dashboard');
}

Available input types

  • Text
  • Textarea
  • Select
  • Radio
  • Checkbox
  • File
  • Range
  • HtmlInputType for the remaining native HTML input types such as email, password, number, date, or time

Each input type configures the underlying Alert instance for you, including inputLabel, inputValue, inputPlaceholder, inputOptions, and inputAttributes when relevant.

Themes

SweetAlert2 ships several themes. Set them directly from PHP:

$alert
->theme(Theme::Auto)
->theme(Theme::Dark)
->theme(Theme::Light)
->theme(Theme::Borderless)
->theme(Theme::Bootstrap5)
->theme(Theme::Bootstrap4)
->theme(Theme::MaterialUI)
;

The optional SweetAlert2 theme styles are not auto-imported. The autoimport section in assets/package.json disables them by default so you can choose the right stylesheet yourself:

"autoimport": {
"sweetalert2/themes/bootstrap-4.css": false,
"sweetalert2/themes/bootstrap-5.css": false,
"sweetalert2/themes/bulma.css": false,
"sweetalert2/themes/material-ui.css": false
}

Retrieving alerts

To access alerts:

$alerts = $alertManager->getAlerts();

Notes

  • Alerts are stored in the session and consumed on read.
  • Alerts must be rendered on the frontend with SweetAlert2-compatible JavaScript.
  • The Stimulus controller dispatches sweetalert:before-fire and sweetalert:after-fire browser events around each Swal.fire(...) call.