Alert

Pretty notifications for user, integrated with Yii message system.

Alert widget displays the messages set via CWebUser.setFlash() using the Twitter Bootstrap Alert widget.

Here's the example:

×Well done! You're successful in reading this.
×Heads up! I'm a valuable information!.
Warning! Check yourself, you're not looking too good.
AAARGHH!!Oh snap! Change something and try submitting again.
// Set up several flashes
// (this should be done somewhere in controller, of course).
$user = Yii::app()->getComponent('user');
$user->setFlash(
    'success',
    "<strong>Well done!</strong> You're successful in reading this."
);
$user->setFlash(
    'info',
    "<strong>Heads up!</strong> I'm a valuable information!."
);
$user->setFlash(
    'warning',
    "<strong>Warning!</strong> Check yourself, you're not looking too good."
);
$user->setFlash(
    'error',
    '<strong>Oh snap!</strong> Change something and try submitting again.'
);

// Render them all with single `TbAlert`
$this->widget('booster.widgets.TbAlert', array(
    'fade' => true,
    'closeText' => '&times;', // false equals no close link
    'events' => array(),
    'htmlOptions' => array(),
    'userComponentId' => 'user',
    'alerts' => array( // configurations per alert type
        // success, info, warning, error or danger
        'success' => array('closeText' => '&times;'),
        'info', // you don't need to specify full config
        'warning' => array('closeText' => false),
        'error' => array('closeText' => 'AAARGHH!!')
    ),
));

Here's all configuration properties which you can set for Alert widget.

Note Usage of the Alert widget configuration properties was presented in an example above.

Property Description
array alerts = array() The configuration for individual types of alerts (see below).
string|false closeText = '&times' What to render as a button to close the alert panel. Default is to render a diagonal cross symbol. If set to false, no close button will be rendered, making user unable to close the alert.
boolean fade = true When set, alert will fade out using transitions when closed.
array events = array() volatile The Javascript event handlers attached to all alert elements being rendered. It should be an array with elements being a javascript string containing event handler function definition (along with declaration) and indexed with the names of events. This will be fed to jQuery.on verbatim.
array htmlOptions = array() Traditional property to set attributes to the element wrapping all of alerts.
string userComponentId = 'user' Name of the component which will be used to get alert messages. It should implement getFlash() method which returns alert message by its type

The alerts configuration property may hold the instructions about how to render individual types of alerts. It is an array indexed by the alert type and with each item being a configuration array for this type of alert.

If the alerts property is not set, all alert types defined in TbAlert::TYPE_* constants will be rendered, with default settings.

Property Description
boolean visible = null If set to false, this type of alerts will not be rendered.
boolean fade = global value The same as a global fade property. If set, alert will close itself fading away. It defaults to the widget-level fade property value.
array htmlOptions = array() Attributes for the individual alert panels. Widget-level htmlOptions was for wrapper element around them. Note that the class attribute will be appended with classes required for alert to be Twitter Bootstrap alert.
string closeText = global value The same as a global closeText property. If set to false, close button will be removed from this type of alert. It defaults to the widget-level closeText property value.

Note Instead of full arrays you can use just the names of alert types as a values of the alerts property. You can even mix the array configuration and plain names. It was shown in the example, by the way.

TbAlert defines a set of class constants to help users of widget define allowed alert types uniformly. Each TYPE_* constant corresponds to some CSS class which will be appended to the alert HTML element. This class mainly just sets the color of the alert background, nothing else. For details see the documentation for Twitter Bootstrap Alert widget.

Name Description
TYPE_SUCCESS Corresponds to alert-success CSS class.
TYPE_INFO Corresponds to alert-info CSS class.
TYPE_WARNING Corresponds to alert-warning CSS class.
TYPE_ERROR Corresponds to alert-danger CSS class.
TYPE_DANGER Corresponds to alert-danger CSS class. It renders the same as TYPE_ERROR by default.