Dynamically add multiple instances of the same model to ActiveForm

828 views Asked by At

I have a form that uses multiple of the same model. When the user clicks "add item" it creates a new row (in this case a elements of a 'model') to ActiveForm. This is important in cases when you have a form where the amount of items needed to be added are unknown and rather than have the user navigate multiple pages I would like to use one form. I found a way to save these elements but currently my method (I'm using renderAjax()) loses its client validation for the new elements.

I would like to know if anyone has had experience in using ActiveForm and Ajaxing in new form elements from models and still retain client validation.

Thanks in advance.

1

There are 1 answers

1
tripskeet On BEST ANSWER

Not exactly a cookie cutter method like some JS frameworks but this works.

Clone this attr:

$('#someForm').yiiActiveForm('find','id');

Then modify and add to ActiveForm:

$('#someForm').yiiActiveForm('add', cloned_attr);

I'm not exactly sure if I messed something up or if its intended this way but validation is working now.