tara
tara

Reputation: 93

flash message in yii2not work when use that with js code

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');
    } 

Upvotes: 0

Views: 1198

Answers (2)

karpy47
karpy47

Reputation: 900

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

Upvotes: 0

Rahul Dhande
Rahul Dhande

Reputation: 483

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'
        );
        ?>

Upvotes: 1

Related Questions