Difference between #1 and #2 of
Using setFlash for displaying various kind of messages

Revision #2 has been created by Trejder on Dec 8, 2010, 11:22:01 AM with the memo:

Incorrect line breaks were removed
« previous (#1)

Changes

Title unchanged

Using setFlash for displaying various kind of messages

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

flash, error, message

Content changed

In this short how-to I will show how to customize _setFlash()_ to display messages (flashes) styled to be success
 
 
confirmation, error report or notice information. And how to achieve displaying errors in other way. ### What do we have? Yii (basic application) uses _setFlash()_ to display messages to user. In the controller there could be a code
 
 
like this:
[...]
```

But this is mainly used to display success confirmation. However, there is a quite easy way to customize this

 
 
behaviour, so we can display also errors and notices as well. ### Solution 1: Magical # char We can encode type of flash message inside flash itself. Here I'm doing it by changing class but this is only
 
 
example, this can be done in any other way.

In the controller:
[...]
```

Now, according to what is a boolean value of _$overall_result_ (where of course we can put any checking, we want),

 
 
user will see either success confirmation or error message. The above (view) code also takes care of situation if there isn't any # char in the flash. If so, it displays
 
 
whole flash message with _flash-success_ class, assuming that there is no class definition, if there is no # char.

### Solution 2: Own function
[...]
```

And user can specify in _htmlOptions_, which class to use in displaying text (_flash-success_, _flash-error_,

 
 
_flash-notice_, all found in _main.css_, used by default Yii application or any other user-defined class). This solution is easier, but it does not make any use of build in flashing feature. I.e. developer has to take his
 
 
own care for transferring such message from the controller to a view.

In the controller:
[...]
### Summary

I hope this short how-to will help some Yii developers. If you find any typo or error in this text, please feel

 
 
free to correct it.
5 4
6 followers
Viewed: 93 086 times
Version: 1.1
Category: How-tos
Written by: Trejder
Last updated by: Trejder
Created on: Dec 8, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history