Problem With Cjuidialog And Ajaxsubmitbutton

I created dialog like in http://www.yiiframework.com/wiki/72/cjuidialog-and-ajaxsubmitbutton/

What i do to get error:

I open dialog (append record) and close it with close button

I open same dialog again and press submit button

Then Addnew action called twice and model saved (appended in database) twice too.

Same behavior if i open dialog, press submit, then open it again, press submit. I get second record created twice because addnew called twice.

Some background:

I used Yii 1.1.8 about year ago. All worked well until i upgraded to yii 1.1.12. I checked it with old version of yii and it works.

I checked upgrade instructions but not found anything than can apply to my case.

Below i show code i use. Sorry for long post and ugly english.

Action:


    

public function actionAddnew() {

        $model=new Test;

        // Ajax Validation enabled

        $this->performAjaxValidation($model);

        // Flag to know if we will render the form or try to add new jon.

        $flag=true;

        if(isset($_POST['Test'])) {       

            $flag=false;

            $model->attributes=$_POST['Test'];

  

            if($model->save()) {

                echo $this->renderPartial('_viewCompact',array('data'=>$model,),false,true);

            }

        }

        if($flag) {

            $headers = getAllHeaders();

            $model->EDUCATIONINF_ID = $headers['EDUCATIONINF_ID'];

            Yii::app()->clientScript->scriptMap['jquery.js'] = false;

            $this->renderPartial('createDialog',array('model'=>$model,),false,true);

        }

    }



create dialog button:




    echo CHtml::ajaxButton(Yii::t('Test','Внутренний экзамен'),

        $this->createUrl('Test/addnew'),

        array(

            'onclick'=>'$("#testDialog").dialog("open"); return false;',

            'update'=>'#testDialog',

            'beforeSend'=>'function(xhr) {

                xhr.setRequestHeader("EDUCATIONINF_ID", document.getElementById("EDUCATIONINF_ID").value);

            }'),

        array(

            'id'=>'showTestDialog',

            'class'=>'AddButton')

    );



CreateDialog.php




<?php 

$this->beginWidget('zii.widgets.jui.CJuiDialog',

    array(

        'id'=>'testDialog',

        'options'=>array(

            'title'=>Yii::t('Test','Добавить внутренний экзамен'),

            'autoOpen'=>true,

            'modal'=>'true',

            'width'=>'auto',

            'height'=>'auto',

        ),

        'cssFile'=>'css/jquery-local.css',

    )

);

echo $this->renderPartial('_formDialog', array('model'=>$model)); 

$this->endWidget('zii.widgets.jui.CJuiDialog');?>



_formDialog.php




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'test-form',

	'enableAjaxValidation'=>true,

)); ?>


    <?php echo $this->renderPartial('__subform', array('form'=>$form,'model'=>$model,'addButtons'=>false,'Hidden'=>true)); ?>

    

    <div class="row buttons">

        <?php 

            echo CHtml::ajaxSubmitButton(Yii::t('Test','Добавить внутренний экзамен'),

                CHtml::normalizeUrl(array('Test/addnew','render'=>false)),

                array('success'=>'js: function(data) {

                        $("#Tests").append(data);

                        $("#testDialog").dialog("close");

                }'),

                array('id'=>'closeTestDialog')

             ); 

        ?>

    </div>

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