SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Tutorials - Big bits of code to help you do more

Setting and displaying custom messages in your site

SSBpostfeedbackmessages

There is a method on SilverStripe's Form class to set a success or error message when the form is submitted. That's cool... but sometimes you need to display a message that is not related to a form. I've created a couple of simple controller methods to enable setting and displaying of message banners anytime.

In Page.php

class Page_Controller extends ContentController {

	public function setMessage($type, $message)
	{	
		Session::set('Message', array(
			'MessageType' => $type,
			'Message' => $message
		));
	}

	public function getMessage()
	{
		if($message = Session::get('Message')){
			Session::clear('Message');
			$array = new ArrayData($message);
			return $array->renderWith('Message');
		}
	}
}

The setMessage() method on the controller takes two arguments; $type is used as the CSS id for the message template. It defines the color and styles for the message. I use one of 3 message types - Error, Success, Notice. You can use whatever message types you want, just add styles accordingly. The $message argument is the message you want to be displayed to the user.

When setMessage() is called, a Message array is stored in Session, ready to be retrieved by, you guessed it... getMessage(). The getMessage method grabs the message if it has been set, removes it from Session (we only want it to display once) and then returns the Message array rendered with the Message template. 

In themes/yourtheme/templates/Includes/Message.ss 

<% if Message %>
	<p class='message' id='{$MessageType}Message'>
		$Message
	</p>
<% end_if %>

First we check if a message has been set, if so, render the HTML to display the message, with a css id that will identify the message type for our CSS styles.

layout.css

p.message {
	padding:10px;
	margin: 10px 10px 0;
	border: 1px solid #FFF;
}

p#SuccessMessage {
	color:#3E933A;
	background:#E2F7D8;
	border-color:#9FDE9C;
}

p#NoticeMessage {
	color:#CCBD00;
	background:#FFF8D7;
	border-color:#F1E988;
}

p#ErrorMessage {
	color: #DA1D1D;
	background: #FFDFDF;
	border-color: #FFBBBB;
}

So that's the guts of it. We can now place $Message in our page template wherever we want the message displayed.

Example usage (in page controller or extension of)

if($foo ! = $bar){
	$this->setMessage('Error', 'Error: Yo fool, foo is not bar');
	$this->redirectBack();
}
  • Peter Toi
    31/10/2011 12:38pm (3 years ago)

    Very simple solution. Thanks!

  • Frank Mullenger
    02/11/2011 11:33pm (3 years ago)

    Hey Shea, I made a module that decorates page_controller and does pretty much the same thing - great minds think alike! :-) https://github.com/frankmullenger/silverstripe-statusmessage

    One problem I came across was setting the session message then a call to header() using Director::redirect() or similar was not passing the session ID or something? Have you noticed this as well?

  • MRKDevelopment
    17/11/2011 3:39pm (3 years ago)

    This is very similar to a flash message in CakePHP. Does anyone know if the new silverstripe have this feature built in ?

    It would certainly be a handy feature to have and most other fraeworks have it.

  • Adam Stead
    11/02/2012 1:47pm (3 years ago)

    We have been doing something similar, except your solution is nice and simple. Love it, great work.

  • Shea
    12/02/2012 12:47am (3 years ago)

    Thanks for the feedback guys.

    @Frank Nice module! No, I havn't had the problem you describe...

    Sounds like a few people are implementing similar solutions... i think it would be awesome if something like this could be added to SS3. Might look into that ;)

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image Martijn van Nieuwenhoven
7 article image Darren-Lee
8 article image Roman Schmid
9 article image Matt Clegg
10 article image dalesaurus

View full leaderboard


Advertisement