PeterParker
(Spamthismail2012)
March 20, 2016, 8:57pm
1
Hi
I have a little problem with flash’s, you can see my question below.
SiteController.php
public function actionCreate()
{
$cat = new Curriculum();
if ($cat->load(Yii::$app->request->post())) {
if ($cat->validate()) {
$cat->save();
Yii::$app->session->setFlash('valid','Added');
return $this->redirect('index.php?r=page');
}
}
return $this->render('create', [
'curriculum' => $cat,
]);
}
index.php
<?php if (Yii::$app->session->getFlash('valid') !== NULL){ ?>
<div class="alert alert-success"><?= Yii::$app->session->getFlash('valid'); ?></div>
<?php } ?>
The output is correct:
i.imgur.com/nSyp9zM.png
But if i use success in setFlash i have wrong output…
SiteController.php
public function actionCreate()
{
$cat = new Curriculum();
if ($cat->load(Yii::$app->request->post())) {
if ($cat->validate()) {
$cat->save();
Yii::$app->session->setFlash('success','Added');
return $this->redirect('index.php?r=page');
}
}
return $this->render('create', [
'curriculum' => $cat,
]);
}
index.php
<?php if (Yii::$app->session->getFlash('success') !== NULL){ ?>
<div class="alert alert-success"><?= Yii::$app->session->getFlash('success'); ?></div>
<?php } ?>
i.imgur.com/2gbeQAN.png
What’s i’m doing wrong?
Thank you
danaluther
(Dana Luther)
March 20, 2016, 10:09pm
2
Once you use getFlash, it’s gone. Your if() statement should be checking hasFlash, or saving the value returned so that it can be displayed.
PeterParker
(Spamthismail2012)
March 20, 2016, 10:55pm
3
The problem is, if run only this code:
Yii::$app->session->setFlash('success','Added');
I get the message…why? You can see in examples above.
softark
(Softark)
March 20, 2016, 11:25pm
4
Could you explain what did you expect and what did you get, not with a screen shot but with some words?
We are very sorry, but we can’t see your screen shot. Due to a spam protection policy, you need to have at least 3 posts before you can post a link in the article.
softark
(Softark)
March 21, 2016, 3:02am
5
getFlash() retains the value within the current session by default. Look at the 3rd parameter "$delete".
http://www.yiiframework.com/doc-2.0/yii-web-session.html#getFlash()-detail
danaluther
(Dana Luther)
March 21, 2016, 12:27pm
6
Gah - My brain flashed back to the 1.0 version!
danaluther
(Dana Luther)
March 21, 2016, 12:32pm
7
Do you have anything else that’s setting the flash with the key of ‘success’? If you use addFlash() instead of setFlash() you can add multiple messages with the same key, but setFlash() will override the key with whatever the last value was.
Hi Peter Parker 007!
Looking at your screenshots I have one question:
Are you using some kind of theme? If yes, which one?
In your second screenshot the flash message is displayed 2 times.
First one => Displayed by your own code.
Second one => Displayed by (i guess) a theme with a "dismiss" button.
This indicates to me that you are using a theme that already handles the standard flash messages (success, error, warning) for you automatically… That is also the reason why it is "ok for you" when you use "valid", because "valid" is not covered by the theme…
You have to look at & change the flash-display at the THEME-files…
Best Regards
PeterParker
(Spamthismail2012)
March 21, 2016, 2:44pm
9
MetaCrawler:
Hi Peter Parker 007!
Looking at your screenshots I have one question:
Are you using some kind of theme? If yes, which one?
In your second screenshot the flash message is displayed 2 times.
First one => Displayed by your own code.
Second one => Displayed by (i guess) a theme with a "dismiss" button.
This indicates to me that you are using a theme that already handles the standard flash messages (success, error, warning) for you automatically… That is also the reason why it is "ok for you" when you use "valid", because "valid" is not covered by the theme…
You have to look at & change the flash-display at the THEME-files…
Best Regards
Theme? I’m not using some theme. (only bootstrap, you can see some code below)
When i use ‘sucess’ the message is displayed 2 times. (i.imgur.com/FKILNtr.png )
If i use only this code (only in Controller, nothing more):
Yii::$app->session->setFlash('success','Added');
The message is displayed 1 time but without verification and in wrong place.
i.imgur.com/Ls4d5Oi.png
I only need add one message and with setFlash should work correctly.
If i use only this code (only in Controller, nothing more):
Yii::$app->session->setFlash('success','Added');
The message is displayed 1 time but without verification and in wrong place.
Thanks for all replys
jscott
(John)
March 21, 2016, 5:10pm
10
Theme? I’m not using some theme. (only bootstrap, you can see some code below)
When i use ‘sucess’ the message is displayed 2 times. (i.imgur.com/FKILNtr.png )
If i use only this code (only in Controller, nothing more):
Yii::$app->session->setFlash('success','Added');
The message is displayed 1 time but without verification and in wrong place.
i.imgur.com/Ls4d5Oi.png
You are using the advanced template and it includes a basic theme, however, in this case it is called the ‘layout’
Check frontend/views/layout/main.php
You will find a code segment like this…
<div class="container">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<?= $content ?>
</div>
</div>
The line
<?= Alert::widget() ?>
is reading some of your flash messages and causing it to display them ‘in the wrong location’.
Try removing this line and see if it behaves more like you expect and let us know.
-John
PeterParker
(Spamthismail2012)
March 21, 2016, 8:24pm
11
JScott:
You are using the advanced template and it includes a basic theme, however, in this case it is called the ‘layout’
Check frontend/views/layout/main.php
You will find a code segment like this…
<div class="container">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<?= $content ?>
</div>
</div>
The line
<?= Alert::widget() ?>
is reading some of your flash messages and causing it to display them ‘in the wrong location’.
Try removing this line and see if it behaves more like you expect and let us know.
-John
If i comment that line works great.
But this line isn’t needed?
Thanks
jscott
(John)
March 21, 2016, 8:57pm
12
Glad it is working now.
The issue is that that line calls a widget that retrieves and displays flash messages.
It does this with some ‘standard’ message types like MetaCrawler mentioned.
If you use one of those types, then the layout displays it in its own place ( the wrong one in your case ), then you turn around and display it again in your content area ( via the render view method ).
This not something most people want. So you can use a standard message type and remove that line from the layout, or you can use a custom message type and put that line back in the layout. It is really your choice.
I usually remove this type of line from the layout, and put it in my content area.
-John