[Solved]Add rows dynamically to CActiveForm


I have an CActiveform to which when i click on ‘+’ it should allow me to add one more row dynamically. Please help me.

I’m assuming this is a JS issue. Please post your code.

Hi waterloomatt,

My CActiveform uses two models, model and model1. When data is input by the user in this fields, onclick of ‘+’ button the form should allow to append only the fields of model1. How do i do this?

I have attached the form screen for reference. I want only the last row elements to append i.e. parameter1,condition,parameter2,operator. The rest of the elements belong to ‘model’ model.

<div class="form">

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



)); ?>

	<?php 	echo $form->errorSummary(array($model));

			echo $form->errorSummary(array($model1));





		<?php echo $form->labelEx($model,'Rule_List_id'); ?>

		<?php echo $form->textField($model,'Rule_List_id'); ?>

		<?php echo $form->error($model,'Rule_List_id'); ?>



		<?php echo $form->labelEx($model,'rule_name'); ?>

		<?php echo $form->textField($model,'rule_name'); ?>

		<?php echo $form->error($model,'rule_name'); ?>


	<td>	.






		<?php echo $form->labelEx($model1,'parameter2'); ?>

		<?php echo $form->dropDownList($model1,'parameter2', CHtml::listData(ruleFrontend::model()->findAll(array('group'=>'parameter2')),'Rule_FrontEnd_id','parameter2'), array('empty'=>'--please select--'));?>

		<?php echo $form->error($model1,'parameter2'); ?>





		<?php echo $form->labelEx($model1,'concat_operator'); ?>

		<?php echo $form->dropDownList($model1,'concat_operator', CHtml::listData(ruleFrontend::model()->findAll(array('group'=>'concat_operator')),'Rule_FrontEnd_id','concat_operator'), array('empty'=>'--please select--')); ?>

		<?php echo $form->error($model1,'concat_operator'); ?>



	<td><?php echo CHtml::Button('+',array('submit'=>'admin'));?>




	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>




<?php $this->endWidget(); ?>

</div><!-- form -->

public function actionCreate()


		$model=new RuleList;

		$model1=new RuleFrontend;



        // populate input data to $model and $model1





        // validate BOTH $model and $model1


        $valid=$model1->validate() && $valid;




            // use false parameter to disable validation






            // ...redirect to another page





    $this->render('create', array(





See an example here: http://sroucheray.org/blog/jquery-dynamic-form/


Thanks Matt will go through the post

Check out jqRelCopy extension?


Thanks dniznick, The extension is gud and works fine. I could create the clone of the row elements now only thing i need to call a method which allows me to save the records on click of the add link.

I may have misinterpreted your question. Are you trying to save them in realtime (one by one) or adding a bunch and then submitting as a batch?

I want to submit as a batch. Could you help me with this?

Did you read the Tabular input section of the guide?


Search my posts - I just solved something similar using batch creation.

Thanks dniznick, I found your post. I am working on implementing a similar logic to my problem.

I should be honest and say that Dana actually solved the problem :)

Hi dniznick,

I have two dependent dropdownlist in my elements to be clonned. When i select an option from first dropdownlist, automatically second gets updated. So when i click on add its clone is formed in which second dropdown shows only those option already popped out first time. If i change the first dropdownlist there is no change in the second. The dependency is no more. I think the controller method is called only once ie the first time. I want to retain this dependency.Is there any way we can get it go?

Hi it’s great that you found a solutions to the above problem. I am faced with the same problem, going through the reply of others i and solutions offered it didn’t help much.

Basically i have two models, Questions and Answers models respectively. on the CActiveform of the Questions model i want to be able to create a question and the dynamically generate 4 input fields for answers to go along with the question.

My problem is similar to yours and i will appreciate to know how Dana was able to solve it. In addition how can generate the 4 input fields without for Answers model without necessarily having the Add button (+) that generates the fields once clicked?

thanks in advance