Cakephp setFlash() Flash() Custom Error messages

In cakephp you can achieve custom style class to Session->flash() method by using layout parameter.
So you can apply different style to messages like “Error”,”Notice” or “Success” message.

In your controller file you might have a typical code like this.

if (!empty($this->data))  {
    $this->Session->setFlash('Form Submitted Correctly.', 'flash_success');
} else {
    $this->Session->setFlash('Please Enter information again.', 'flash_error');

The “flash_success” and “flash_failure” parameter represents a layout file. so you have to create two layout files in your layouts folder at “app/views/layouts”.

So inside the flash_success.ctp you can add code like this

echo "<div class=\"flash flash_success\">{$content_for_layout}</div>";

and inside the flash_error.ctp you can add code like this

echo "<div class=\"flash flash_error\">{$content_for_layout}</div>";

And the final step is to display those Session->flash() messages in your actual view file.
you can add following code at the place where you want to display the error/success messages.

<?php $session->flash(); ?>

And last add css classes in your CSS file like .flash_error,.flash_success

Leave a Reply

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


  1. Excellent tutorial, thanks for sharing it….

  2. Shawna Caramella says:

    Thx for information.