Back to Top

Notifications in PrestaShop Symfony Controllers

Updated 28 January 2026

Introduction

In PrestaShop Back Office module development, here we are learn how to show notifications in PrestaShop Symfony controllers.

Validation errors are often handled by throwing PHP exceptions in Symfony based Controllers.

While this approach works, it can interrupt the user experience and display unfriendly error pages.

A better approach is to use Symfony Flash Messages to display validation like success, error, or other notifications (alerts) feedback directly in the Back Office interface.

Flash messages allow you to show success, error, warning, and info messages without breaking the workflow.

Searching for an experienced
Prestashop Company ?
Find out More

In this article, we will learn how to show notification messages in a PrestaShop Symfony controller (PrestaShop Back Office Symfony controllers) using flash messages instead of PHP exceptions.

Why Use Flash Messages Instead of Exceptions?

Using flash messages offers several advantages:

  • Improves user experience
  • Prevents application crashes
  • Keeps users on the same page
  • Displays messages in a consistent UI format
  • Works well with PrestaShop’s Symfony-based Back Office

Instead of stopping execution with an exception, we gently notify users about issues and guide them to fix them.

Use Case Example

In this tutorial, we will validate whether a product reference number already exists while saving a product in the Back Office.

If the reference is duplicated, we will:

  • Prevent the save
  • Display an error message
  • Redirect the user back to the product edit page

Step 1: Register a Hook

PrestaShop provides hooks to interact with the Back Office form handling process.

We use:

actionBeforeUpdateProductFormHandler

This hook is triggered before the product form is saved.

Make sure it is registered in your module’s install() method:

$this->registerHook('actionBeforeUpdateProductFormHandler');

Step 2: Create the Validation Method

Now, create a helper method to check if the reference already exists.

public static function isReferenceExist($reference, $productId)
{
    $sql = new DbQuery();

    $sql->select('id_product')
        ->from('product')
        ->where('reference = "' . pSQL($reference) . '"')
        ->where('id_product != ' . (int) $productId);

    return (bool) Db::getInstance()->getValue($sql);
}

This method checks if another product already uses the same reference.

Step 3: Implement the Hook Method

Create the following method inside your module class:

public function hookActionBeforeUpdateProductFormHandler($params)
{
    $idProduct = (int) Tools::getValue('id_product');
    $reference = $params['form_data']['details']['references']['reference'] ?? '';

    if (!$reference || !$this->isReferenceExist($reference, $idProduct)) {
        return;
    }

    // Get session
    $container = PrestaShop\PrestaShop\Adapter\SymfonyContainer::getInstance();

    // Get session (PS < 9 / PS >= 9 compatible)
    $session = version_compare(_PS_VERSION_, '9.0.0', '>=')
        ? $container->get('request_stack')->getSession()
        : $container->get('session');

    // Add flash error
    $session->getFlashBag()->add(
        'error',
        $this->l('Reference number is duplicate')
    );

    // Redirect
    Tools::redirectAdmin(
        $this->context->link->getAdminLink('AdminProducts', true, [
            'id_product' => $idProduct,
            'updateproduct' => 1,
        ])
    );
}

You can check the image for the references.

Show Validation in PrestaShop Symfony Controllers
Show Validation in PrestaShop Symfony Controllers

Step 4: Understanding Flash Messages

Symfony flash messages are stored in the session and displayed on the next request.

PrestaShop automatically renders them in the Back Office.

You can add different types:

$session->getFlashBag()->add('success', 'Saved successfully');
$session->getFlashBag()->add('error', 'Something went wrong');
$session->getFlashBag()->add('warning', 'This action may have side effects');
$session->getFlashBag()->add('info', 'Changes will apply after cache clear');
flag types
Types of flash messages

Step 5: Redirecting After Validation

After adding flash messages, you must redirect the user back to the edit page:

Tools::redirectAdmin($redirectUrl);

This is important because flash messages appear only after a redirect.

Without redirection, the system may not display messages correctly

How It Works Internally

Here’s what happens behind the scenes:

  1. User submits the product form
  2. Hook is triggered
  3. The module validates the reference
  4. The system detects an error
  5. The module stores the message in the session
  6. The system redirects the user
  7. PrestaShop displays the message

This creates a smooth validation flow without exceptions. It aligns with PrestaShop’s modern architecture and best practices.

Conclusion

Handling validation errors using flash messages in PrestaShop Back Office is a modern and user-friendly approach.

Instead of breaking the workflow with PHP exceptions, you can:

  • Store messages in the Symfony session
  • Redirect users gracefully
  • Display clean UI notifications

This improves both usability and maintainability of your modules.

That’s all about this blog. Hope it will help you.

If you are facing any issues or have any doubts about the above process, please feel free to contact us through the comment section.

Also, you can explore our Prestashop Development Services and a large range of quality Prestashop Modules.

For any doubt, contact us at [email protected]

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home