flash message in yii2not work when use that with js code

1.1k views Asked by At

hi i have flash message to show success message and i want hide that after 3 seconds . i use js code but in line 3 when i use view::POS_READY i get error to not found that class so i comment this line and after that js code not worked and not fade my message. how can fix this problem?

this is my show flash code in view:

<?php if(Yii::$app->session->hasFlash('flashMessage')):?>
    <div class="flash-success">
            <?php echo Yii::$app->session->getFlash('flashMessage'); ?>
            <?php
            $this->registerJs(
            "$('.flash-success').animate({opacity: 1.0}, 3000).fadeOut('slow');",
            //view::POS_READY,
            'myHideEffect'
);
            ?>

set flash in controller:

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        Yii::$app->session->setFlash('flashMessage', 'success');
 return $this->redirect('index.php');
    } 
2

There are 2 answers

0
Rahul Dhande On BEST ANSWER

Try this... or you can "use \yii\web\View;" on top of the view file

<?php if(Yii::$app->session->hasFlash('flashMessage')):?>
    <div class="flash-success">
        <?php echo Yii::$app->session->getFlash('flashMessage'); ?>
        <?php
            $this->registerJs(
            "$('.flash-success').animate({opacity: 1.0}, 3000).fadeOut('slow');",
            \yii\web\View::POS_READY,
            'myHideEffect'
        );
        ?>
0
karpy47 On

The accepted answer probably works great, but I would like to propose using the Growl widget. Fading is just a part of the features.