GSTAR
(Omzy83)
October 4, 2010, 5:39pm
1
Controller:
if($model->save())
{
Yii::app()->user->setFlash('success', "Your changes were saved successfully");
$this->redirect(array('view'));
}
view.php:
<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="flash-success">
<?php Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
The div container is displayed but the message is not. I have inspected using Firebug and the message is definitely not there.
GSTAR
(Omzy83)
October 4, 2010, 7:59pm
2
Anyone got any idea what the problem could be?
bettor
(Live Webscore)
October 4, 2010, 8:01pm
3
Yes. You need to echo your message such as:
<?php echo Yii::app()->user->getFlash('success'); ?>
Hope this helps
GSTAR
(Omzy83)
October 4, 2010, 8:16pm
5
Any way to simplify this code:
<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
It just seems very long and untidy.
bettor
(Live Webscore)
October 4, 2010, 8:24pm
6
I would guess…design a separate class or static method to handle just Flash messages through attributes and the attribute being the Flash id…I would also be interested to hear what other people have invented in this direction.
cyberpol
(Cyberpol 777)
October 4, 2010, 8:26pm
7
One idea is to have a shortcuts functions (read this cookbook)
and add this function:
function printFlash($flash,$div_flash_class = 'flash-success'){
if(Yii::app()->user->hasFlash('success')){
echo '<div class="'.$div_flash_class.'">';
echo Yii::app()->user->getFlash('success');
echo '</div>';
}
}
And use it
printFlash('success'); //in your case, nothe that you can pass the class to aply to the div too...
bettor
(Live Webscore)
October 4, 2010, 9:04pm
8
Excellent idea PoL. Thanks
GSTAR
(Omzy83)
October 4, 2010, 9:56pm
9
How about putting the code in another view file and then displaying it using renderPartial()?
cyberpol
(Cyberpol 777)
October 5, 2010, 12:16pm
10
Is not a bad Idea, in fact is the same principle: reusability.
Y11
(Y!!)
October 5, 2010, 12:25pm
11
I do this:
<?php $this->widget('FlashMessageWidget'); ?>
And in order to set:
Yii::app()->user->setErrorFlash()
Yii::app()->user->setNoticeFlash()
Yii::app()->user->setSuccessFlash()
bettor
(Live Webscore)
October 5, 2010, 1:19pm
12
Have you extended CWebUser? Can you elaborate a bit further on your implementation. It looks very interesting
Y11
(Y!!)
October 5, 2010, 1:35pm
13
Yes extended with a subclass. Like:
public function setErrorFlash($title, $message)
{
$this->setFlash('error', array('title' => $title, 'message' => $message));
}
Note that the second argument of CWebUser::setFlash() can be anything.