ajaxSubmitButton Issue since 1.1.2

Hey guys,

I’m having a strange problem since upgrading from Yii 1.1 to 1.1.2 or even the latest release 1.1.3.

Using the following code


<div class="form">

<?php if(isset($err)) { echo $err; } ?>

<?php echo CHtml::beginForm($this->createUrl('usersComments/ajaxCreate')); ?>

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


	<?php echo CHtml::errorSummary($model); ?>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'comments_data'); ?>

		<?php echo CHtml::activeTextArea($model,'comments_data',array('rows'=>6, 'cols'=>50)); ?>

		<?php echo CHtml::error($model,'comments_data'); ?>

	</div>


	<div class="row buttons">

    <label for='submit'></label>

		<?php echo CHtml::ajaxSubmitButton('Comment',$this->createUrl('usersComments/ajaxCreate'),array('update'=>'#users-comments')); ?>

	</div>

<?php echo CHtml::activeHiddenField($model,'comments_owner',array('value'=>$user->users_id)); ?>

<?php echo CHtml::endForm(); ?>

</div><!-- form -->

<div style='height:10px;'></div>

and controller;


    //function to make comment ajax

    public function actionAjaxCreate()

    {

        $model=new UsersComments;

        if(Yii::app()->request->isAjaxRequest)

        {

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

            //make comment, load data

            if($model->save())

            {

                //load data

                $ph=Users::model()->findByPk($model->comments_owner);

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

            }

            else

            {

                $err="Don't forget to write a comment";

                //load data

                $ph=Users::model()->findByPk($model->comments_owner);

                $this->renderPartial('_ajax_comments',array('model'=>$ph,'err'=>$err),false,true);                

            }

        }

    }

is executing a random amount of times.

If I use Yii 1.1 it’s fine, it only executes/submits once.

Problem only occurs if the form is rendered via an CHtml::ajaxLink call.

Is there a bug with 1.1.2 onwards that is causing Ajax to submit multiple times?

Cheers,

Snorcha

I think I’ve narrowed it down to this code that is generated by ajaxsubmitbutton;

This is the 1.1.2 onwards version:




jQuery('#yt0').live('click',function(){jQuery.ajax({'type':'POST','url':'/slapntickle/usersComments/ajaxCreate','cache':false,'data':jQuery(this).parents("form").serialize(),'success':function(html){jQuery("#users-comments").html(html)}});return false;}); 



This is the 1.1 version:




jQuery('#yt0').click(function(){jQuery.ajax({'type':'POST','url':'/slapntickle/usersComments/ajaxCreate','cache':false,'data':jQuery(this).parents("form").serialize(),'success':function(html){jQuery("#users-comments").html(html)}});return false;});



Am I the only one having these issues??

lol replying again cause I LOVE talking to myself…

One way to fix it is to alter the code of framework/web/helpers/CHtml.php on line 1861;


protected static function clientChange($event,&$htmlOptions,$live=true)

to


protected static function clientChange($event,&$htmlOptions,$live=false)

I hate altering the base code tho, is there a way to get around this?

This is a common problem.

In general the




 $this->renderPartial('_ajax_comments',array('model'=>$ph,'err'=>$err),false,true);



Have lot of problem. In primis the one you noticed, and there can be other id-mismatch if your button need to change behaviour after the ajax-call.

I proposed here an idea of possible solution. In practice it renders the javascript needed from the partial view directly in the partial view itself.

Hand writing the js code is in general a common workaround for this clientScript problem, my proposal is simply a way for authomatise this concept.