One form for 3 model, where each model can have multiple rows

I’m creating a simple survey application where there is just one form for a survey model, question model and options model.

Each survey can have multiple questions and each question can have multiple options.

Here’s the view file I’m trying to create:

<div class="form">

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



)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

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

	<div class="row">

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

		<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>500)); ?>

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


	<div id="questions-options">

		<div id="questions-boxes">

			<?php echo $form->labelEx($qmodel, 'Question');?>

			<?php echo $form->textArea($qmodel,'text[]',array('rows'=>6, 'cols'=>50)); ?>


			<div id="options-box">

				<?php echo $form->labelEx($omodel, 'Options');?>

				<?php echo $form->textArea($omodel,'[0]text[options][]',array('rows'=>6, 'cols'=>50)); ?>





<!--Autogenerate the URL, so commented

	<div class="row">

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

		<?php echo $form->textField($model,'url',array('size'=>60,'maxlength'=>250)); ?>

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



	<a href="" onclick="addoption(); return false;">Add Question</a>

	<div class="row buttons">

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


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

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


var i=2

function addoption(){

	var el = document.createElement('textarea');






	//var el2 = el.cloneNode(true);



	var a = document.getElementById("questions-boxes").cloneNode(true);





I’ve successfully implemented the multiple questions part, but was unable to do the multiple options for each question part.

I dont know how to link each option to a question in HTML form. I can ofcourse use arrayed parameters similar to following:

<textarea name="question[1][text]></textarea>

<textarea name="question[1][options][]></textarea>

<textarea name="question[1][options][]></textarea>

But then I can’t use validation features etc.

I think model and controller wouldn’t be necessary as I’m trying to figure out how to create a form with such a relation. But if you guys need it, let me know. I’ll be glad to post it.