0

I have a problem displaying Flash messages in Yii. Inside my view I have an ajax button, calling method update of my controller. Inside the update method I want to set a Flash message and display it inside my view when it's updated with new data.

   *update.php :*

    <?php
    <h1>Update Campaign <?php echo $campaign->id; ?></h1>
    <?php
    $tabList = array();
//FORM IS DISPLAYED INSIDE A JUI TAB:
    $tabList['General'] = $this->renderPartial('_form', array('campaign'=>$campaign),true);*

    ...

    $this->widget('zii.widgets.jui.CJuiTabs',array(
        'tabs'=>$tabList,
        'options'=>array(
            'collapsible'=>false,
        ),
    ));
    ?>


*_form.php:*

//HERE I WANT TO DISPLAY A FLASH MESSAGE WHEN _form IS RENDERED:
    <?php foreach(Yii::app()->user->getFlashes() as $key => $message) : ?>
        <div class="flash-<?php echo $key; ?>"><?php echo $message; ?></div>
    <?php endforeach; ?>

    <?php
        Yii::app()->clientScript->registerScript(
            'myHideEffect',
            '$(".flash-success").animate({opacity: 1.0}, 1000).fadeOut("slow");',
            CClientScript::POS_READY
        );
    ?>
    <div class="form">
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'campaign-form',
        'enableAjaxValidation'=>false,
    )); ?>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary($campaign); ?>

        <div class="row">
            <div class="span2">
                    <?php echo $form->labelEx($campaign,'campaign_mode_id'); ?>
            </div>
            <div class="span2">
                <?php echo $form->dropDownList($campaign,'campaign_mode_id', CampaignMode::model()->getModes());?>
                <?php echo $form->error($campaign,'campaign_mode_id'); ?>
            </div>
        </div>
    ...
        <div class="row buttons">
            <div class="span2">
                <?php
                    if($campaign->isNewRecord){
                        echo CHtml::submitButton( 'Create');
                    }else{
//THIS IS MY AJAX-SUBMIT BUTTON, IT CALL CONTROLLER'S UPDATE METHOD AND UPDATE JUI TAB (DIV WITH ID '#yw0_tab0')
                        echo CHtml::ajaxSubmitButton(
                            'Save',
                            Yii::app()->createUrl("//campaign/update/{$campaign->id}"),
                            array('beforeSend' => 'function(){
                                          $("#surveyquestions").addClass("ajaxloading");}',
                                'complete' => 'function(){
                                          $("#surveyquestions").removeClass("ajaxloading");}','update' => "#yw0_tab0"),
                            array('id' => 'send-link-'.uniqid()));
                    }
                ?>
            </div>
        </div>

    <?php $this->endWidget(); ?>


*Contorller:*


    public function actionUpdate($id)
        {
            $campaign=$this->loadModel($id);
            if(isset($_POST['Campaign']))
            {
                $campaign->attributes=$_POST['Campaign'];
                if($campaign->save())
                {
//HERE I'M SETTING THE FLASH MSG
                    Yii::app()->user->setFlash('success','Campaign is updated');

                    if(Yii::app()->request->isAjaxRequest){
//AND UPDATING MY VIEW
                        $this->renderPartial('_form', array('campaign'=>$campaign), true,true);
                    }else{
                        $this->redirect(array('update','id'=>$campaign->id));
                    }
                }
            }
            $this->render('update',array('campaign'=>$campaign));
        }
RB_
  • 1,195
  • 15
  • 35
  • I tried to use renderPartial with different parameters, but it doesn't make any difference, and when I'm walking through the code with xdebug, I can see that these lines are executed after update method call :`user->getFlashes() as $key => $message) : ?>
    `, but flash message is never displayed for some reason.
    – RB_ Jan 31 '14 at 10:18

1 Answers1

0

Actually you are rendering _form view, but without sending it to output (take a look at third param), and then you are rendering update view... The flash message has been rendered once in _form, so it won't be rendered again.

You should simply try this :

if(Yii::app()->request->isAjaxRequest) {
    $this->renderPartial('_form', array('campaign'=>$campaign), false, true);
    Yii::app()->end();
} else {
    $this->redirect(array('update','id'=>$campaign->id));
}
soju
  • 25,111
  • 3
  • 68
  • 70
  • Thanks for your reply, but your solution doesn't work either. In case of using ajax calls update view is rendered only once when form is loaded in the first time. And afterwards, I'm trying to update a single
    that contains only _from. And I also tried to use renderPartial with different parameters/ parameters combinations like true, true; true, false etc. It doesn't work. I can of course make a workaround using js, but I would like to get it working yii way.
    – RB_ Jan 31 '14 at 11:52
  • What does your ajax request return ? Did you check your js/network console for errors ? – soju Jan 31 '14 at 12:36
  • It returns a big HTML that contains _form html with my + another peace of HTML attached to the bottom, that contains rendered update.php with all jui tabs and everything. – RB_ Jan 31 '14 at 15:08
  • When I'm using your code. It returns correct peace of HTML with message on the top. – RB_ Jan 31 '14 at 15:16