Submit CActiveForm with AJAX and validateOnSubmit

(1) I need to submit a form with AJAX, still have it validated on submit. ajaxSubmitButton does not provide that.

(2) Since it’s an ajax form the user should (?) be able to submit it multiple times while staying on the same page.

I really don’t know for sure if this is a solved problem, but sure I can’t find something good enough anywhere I looked.

(1) Solving (1) alone is pretty simple, not the "yii way" though, using the standard submitButton and


$('#my-form').bind('submit', function() {

   return false;

});

(2) By solving (1) the way described above I had the problem that when the form was submitted once it was marked as "validated", thus submitted (jquery.yiiactiveform.js line 146), and hitting submit again would not submit the form again (jquery.yiiactiveform.js line 130). This is not what you want when building an ajax shopping cart in my case, where user may want to add the same product multiple times, in different colors and sizes in example. Adding


$form.trigger('reset');

validated = false;

at jquery.yiiactiveform.js line 154 after having the form submitted allows for the form to be submitted multiple times.

Is there a clean way, without the need for external js & modifying core scripts, to achieve such behavior?

I’m facing this problem too.

Framework version 1.1.10 using.

Same problem here. Seems only way to fix this is re-running the javascript that produces the activeform, which is no a real fix.

By the way, i fixxed it by making my own version and adding the ‘validated = false’ in the reset function. And by doing this in my controller:

Yii::app()->clientScript->scriptMap[‘jquery.yiiactiveform.js’] = ‘/js/yii/jquery.yiiactiveform.js’;

But then i have to update my .js when there is a new release of yii, so still searching for a nicer solution! :slight_smile:

I did this on a per page bases:


<script type="text/javascript">

$(document).ajaxSuccess(function(){

    $('#formId')[0].reset();

});

</script>