Jozsef Naghi
Jozsef Naghi

Reputation: 1105

Implementing flash messages in Yii framework

This is my function from my controller :

public function actionUser(){
        Yii::app()->user->setFlash('success', "Data1 saved!");
....
}

In the view If I write this :

<?php echo Yii::app()->user->getFlash('success'); ?> 

it works, BUT I want to appear somewhere above the content and then automatically fade out after a few second. I tried this from here

<?php
Yii::app()->clientScript->registerScript(
   'myHideEffect',
   '$(".info").animate({opacity: 1.0}, 3000).fadeOut("slow");',
   CClientScript::POS_READY
);
?>

But i don't understand who is myHideEffect and the class .info ? Can someone give me an example ? or a link to a demo? thanks .

Upvotes: 1

Views: 1676

Answers (2)

hamed
hamed

Reputation: 8033

This is a jquery based question, not yii based. However try this:

<?php if(Yii::app()->user->hasFlash("success")): ?>
      <div id="message">
          <?php echo Yii::app()->user->getFlash('success'); ?>
      </div>
<?php endif; ?>

...

<script>
   $(document).ready(function() {
       setTimeout(function() {
           $('#message').fadeOut('slow');
       }, 3000);
   });
</script>

Upvotes: 0

chapskev
chapskev

Reputation: 982

Below there are two implementation for the flash message, We can show messages using toastr notification or the normal bootstrap flash messages implementation.

If you want display messages as toastr notification have this line of code your controller action.

YII::app()->user->setFlash('toastr.error', 'An Error occured when saving');

If you want to use the normal bootstrap flash message use instead.

YII::app()->user->setFlash('alert alert-danger', 'An Error occured.');

For this to work you have to handle the flash messages on your main view layout as below. Most likely just before <?php echo $content; ?>

    /** Takes care of the flashmessages **/
    $flashMessages = Yii::app()->user->getFlashes();
    if ($flashMessages) {

        foreach ($flashMessages as $key => $message) {
          $pattern = '/\s/';
            $match = preg_match($pattern, $key);/* This checks the type of error message to use if the error $key is just one word then use toastr notification */
            if ($match == 0) {
                Yii::app()->clientScript->registerScript(
                        'myNotifyEffect', $key . '("' . $message . '");', CClientScript::POS_READY
                );
            } elseif ($match > 0) {

                if ($key != 'alert alert') {
                    Yii::app()->clientScript->registerScript(
                            'myHideEffect', '$(".' . $key . '").animate({opacity: 1.0}, 5000).fadeOut("slow");', CClientScript::POS_READY
                    );
                    echo '<div class= "' . $key . ' alert-bold-border square fade in alert-dismissable">' . '<button class="close" data-dismiss="alert" type="button">×</button>' . $message . "</div>\n";
                } else {
                    echo '<div class="' . $key . ' alert-bold-border square fade in alert-dismissable">' . '<button class="close" data-dismiss="alert" type="button">×</button>' . $message . "</div>\n";
                }
            }
        }
    }

Upvotes: 2

Related Questions