-I have a CActiveFrom with tabular inputs, configured with ‘enableAjaxValidation’ => true
-My goal is to dynamically add inputs when pressing on a ‘Add button’. This is easy with Chtml::ajax() and a partial
-My issue is that the form does not know that inputs have been added, because the new, ajax-appended inputs were not there when the form widget was created.
How do I re-init $(’#the-form’).yiiactiveform() so it gets to validate the full list of attributes, the old ones and the ajax-appended ones?
I tried to call $(’#the-form’).yiiactiveform() from the FireBug console with a different set of parameters, it breaks the validation by disabling it.
Please take note again that my aim is full ajax, both on validation and on appending new form fields.
-rewrite or patch(see below) Yiiactiveform.jquery.js to allow it to be ‘attched’ multiple times without duplicating event handlers. In you don’t, you’ll get multiple posts when validating, generally multiple event triggers
-register this JS:
$('body').ajaxSuccess(function(e, xhr, settings) {
// you may copy the portion below (from here to the #custom comment) from the 'standard' Yii generated JS
// you may also replace the CSS id selector with a class selector, so you cover multiple instances ofthe same form
$('#my_form').yiiactiveform({'validateOnSubmit':true,
//...
});
// #custom
});
This will enable you to load Yii active forms partials with ajax, and still have clientValidation and ajaxValidation working
The patch unbinds change and keyup ebvents for controls, as well as submit event for the form.
If you want to manage this events, you’ll have to put your js at #custom, after yiiactiveform has been re-initialized