[Solved]Add rows dynamically to CActiveForm

Hi,

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(

	'id'=>'rule-list-form',

	'enableAjaxValidation'=>false,

)); ?>


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

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

	?>

<table>

<tr>

	<!--<td>	

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

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

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

	</td>-->

	<td>	

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

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

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

	</td>

	<td>	.

                .

                .

		.

	<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'); ?>

	

	</td>

	<td>

	

		<?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>

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

</td>

</tr>

</table>	

	<div class="row buttons">

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

		

	</div>

	

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


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




public function actionCreate()

	{

		$model=new RuleList;

		$model1=new RuleFrontend;

    if(isset($_POST['RuleList'],$_POST['RuleFrontend']))

    {

        // populate input data to $model and $model1

        $model->attributes=$_POST['RuleList'];

		//$model1->Rule_List_id=$model->Rule_List_id;

        $model1->attributes=$_POST['RuleFrontend'];

 

        // validate BOTH $model and $model1

        $valid=$model->validate();

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

 

        if($valid)

        {

            // use false parameter to disable validation

			if($model->save(false))

			{

				$model1->Rule_List_id=$model->Rule_List_id;

				$model1->save(false);

				$this->redirect(array(admin));

            // ...redirect to another page

			}

        }

    }

 

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

        'model'=>$model,

        'model1'=>$model1,

    ));

	}

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

Matt

Thanks Matt will go through the post

Check out jqRelCopy extension?

http://www.yiiframework.com/extension/jqrelcopy/

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?

http://www.yiiframework.com/doc/guide/1.1/en/form.table

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