phpjqueryvalidationyiijquery-forms-plugin

Yii client-side validation and jQuery Form plugin integration


I'm using Yii framework in one of my projects and I would like to use the jQuery Form plugin together with Yii client-side built-in validation.

I can't get them work together. If I setup jQuery form plugin with this simple js code:

$('#myform-id').ajaxForm();

the client-side validation is performed but it doesn't stop the form submission even if the validation fails. I suppose that this is related to the fact that both Yii client-side validation library and jQuery form plugin bind the same "submit" event on the form.

FYI, I double checked that there no js errors with FireBug and Chrome console.

I wonder if someone experienced the same issue and solved that someway.


Solution

  • I solved this way:

    Yii Active Form init code:

    <?php $form = $this->beginWidget('CActiveForm', array(
        'id'=>'user-form',
        'enableClientValidation'=>true,
        'clientOptions' => array(
             'validateOnSubmit'=>true,
             'validateOnChange'=>false,
             'afterValidate'=>'js:submiAjaxForm'
         )
    )); ?>
    

    And in the same page I've added this js code to submit the form via jquery form plugin:

    function submitAjaxForm(form, data, hasError)
    {
       if(!hasError)
       {
           $('#user-form').ajaxSubmit(
           {
               // ajax options here
           });
       }
    }