Fields From Other Model In Form


i have the following models:

  • assignment(id, studentID, behaviorID, …)

  • Student(id, name, classID, …)

  • Class(id, name, …)

in assignment/_form

i need to add a drop down list about all the classes in the database

so i write something like this:

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




							echo CHtml::dropDownList('studentClass','studentClass', CHtml::listData(Stclass::model()->findAll(), 'Class_Code', 'E_Class_Abbreviation'), array('empty'=>'Select Class'));

							//echo $form->dropDownList($model,'BehavType', array('1' => 'Positive Behavior', '2' => 'Negative Behavior', '3' => 'All Behavior Types'), array('empty' => '(Select a Behavior Type)'));						

							echo $form->dropDownList($model,'BehavType', array(Yii::app()->params['bType']));

							echo CHtml::button('Generate', array('submit' => array('Assignment/classReport')));



in Assignment controller, classReport action:

		public function actionClassReport()


		$behaviorType = $_POST['Assignment']['BehavType'];

		$stClass = $_POST['Assignment']['studentClass'];

		$this->redirect(array('site/page/view/classReport', 'BehavType'=>$behaviorType, 'studentClass'=>$stClass));


there is an error: undefined index: studentClass

can someone tell me how to pass such a parameter?

thank you

Have you checked CHtml::dropDownList() class reference?

The error you get indicates that the second parameter you passed is incorrect.

i changed the dropdownlist to this in the view:

echo CHtml::dropDownList('Class_Code','Class_Code', CHtml::listData(Stclass::model()->findAll(), 'Class_Code', 'E_Class_Abbreviation'), array('empty'=>'Select Class'));

in controller i call it:

$stClass = $_POST['Class_Code'];

and it works fine

thank you

I’m glad you solved your issue, but I must say I was wrong in my answer, as you’ve found by yourself, sorry.