Double Submit When Validateonsubmit Is True

Hi

I made a custom ajax submit form.

The problem occurs when set the ‘validateOnSubmit’ to true as parameter in CActiveForm.

My ajax form seems like that


 $('#id-form').submit(function() {

                    data = $(this).serialize();

                    alert("a1");

                    $.ajax({...)

                    return false;

});

The form submitted twice.

So, I want to know why my function called twice.

I found a solution using an extra parameter CActiveForm


'afterValidate' => 'js:function() { return false;}'

But, why sould be set it ?

Thanks

Someone please?

I just face this problem again today …

No one has similar issue ?

Maybe this:




$('#id-form').submit(function(event){

    // prevent usual process of submission

    event.preventDefault();


    data = $(this).serialize();

    $.ajax({

        ...

    });

});



After of few months I face the same problem

Actually the calling is:


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

                'id' => 'my-form',

                'enableClientValidation' => true,

                'clientOptions' => array(

                    'validateOnSubmit' => true, //if true then I have to write the below code to avoid twice calling

                    'afterValidate' => 'js:function() { return false;}',

                ),

                

                'htmlOptions' => array('onsubmit' => '

                             run_my_function(); //set a frame source

                             return false;

                            '),

            ));

In this way works… but is it the right way ?

First call is the validation call and this call will add automatically a param, that is the ajax param. the second call does not add this param, therefore, on your controller, you check for the param existance and if it exists, you run only the validation, otherwise, you let it pass and reach the submission code of your controller. This is perfectly normal.

Here’s a sample controller code to illustrate the above:




$request     = Yii::app()->request;

$paymentForm = new StripePaymentForm('validation');

// $paymentForm->modelName is not generated by yii, it's something custom

if($request->isAjaxRequest && $request->isPostRequest && $request->getPost('ajax') == strtolower($paymentForm->modelName)) {

        // this is step one, only the validation

	echo CActiveForm::validate($paymentForm); 

	Yii::app()->end();

}

// this is step two, the save action

$paymentForm->attributes = (array)$request->getPost($paymentForm->modelName, array());

if ($paymentForm->save()) {

    // saved

} else {

    // not saved

}