create form & dropdown list problem

Hi,

This problem is probably very easy for most of you but it took lots of time for me and couldn’t solve it yet. I have two dropdown lists which are campus,building.Building is dependent on campus.I used ajax to solve dependencies.In my location table, there are three columns which are id,campus,building. I use these dropdown lists to add an “event”. Event table holds only the location id which corresponds to id in location table. When I select campus and building on dropdown lists in create form of “event” and click on create button, it says “Please fix the following input errors: campus cannot be blank” and I lost the inputs of campus and building after refresh.

"_form" view:


	<div class="row">

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

		<?php echo CHtml::dropDownList('campus','', CHtml::listData(Location::model()->findAll(), 'campus', 'campus'),

				array(

					'empty' =>'--please select--',	

					'ajax' => array(

					'type'=>'POST', //request type

					'url'=>CController::createUrl('dynamicBuildings'), //url to call.

					//Style: CController::createUrl('currentController/methodToCall')

					'update'=>'#building', //selector to update

					//'data'=>'js:javascript statement' 

					//leave out the data key to pass all form values through

					))); 

		

		 ?>

		<?php echo $form->error(Location::model(),'campus'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx(Location::model(), 'building'); ?>

		<?php echo CHtml::dropDownList('building','', array(), array('empty' =>'--please select campus first--')); ?>

		<?php echo $form->error(Location::model(),'building'); ?>

	</div> 



EventController:


	

public function actionDynamicBuildings()

	{

		//please enter current controller name because yii send multi dim array

		$data=Location::model()->findAll('campus=:campus',

				array(':campus'=> $_POST['campus']));

	

		$data=CHtml::listData($data,'id','building');

		if($data == null){

			echo CHtml::tag('option',

					array('empty'),CHtml::encode('--please select campus first--'),true);

		}

		else{

			echo CHtml::tag('option',

					array('empty'),CHtml::encode('--please select--'),true);

		}

		foreach($data as $value=>$building)

		{

			echo CHtml::tag('option',

					array('value'=>$value),CHtml::encode($building),true);

		}

	}



What can be the problem?

Thanks

There is maybe a problem in your rules() method in your models. Can you please post your rules() methods of the invoked models…

Can you show your create event action from your controller? It looks like your create event action accepts parameters from your create form not correctly. Try to use firebug to see what parameters are sent to your controller when you click ‘create’ button.

rules of event model:




	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('title, details, locationId, typeId, languageId, locationDetail, userId, name, phone, email, date, startHour', 'required'),

			array('locationId, typeId, languageId, correspondingEventId, pictureId, status', 'numerical', 'integerOnly'=>true),

			array('rate', 'numerical'),

			array('title', 'length', 'max'=>128),

			array('userId', 'length', 'max'=>10),

			array('name, email', 'length', 'max'=>64),

			array('phone', 'length', 'max'=>32),

			array('status', 'in', 'range'=>array(0,1)),

			array('email', 'email'),

			//array('date', 'date'),

			array('endHour', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('title, details, locationId, typeId, languageId, locationDetail, userId, name, phone, email, correspondingEventId, date, startHour, endHour, pictureId, rate, status', 'safe', 'on'=>'search'),

					);

	}



rules of location model:




	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('building, campus', 'required'),

			array('building', 'length', 'max'=>128),

			array('campus', 'length', 'max'=>32),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, building, campus', 'safe', 'on'=>'search'),

		);

	}



I used firebug to check ajax functionality but it seemed ok to me. When I choose campus first, it responses with corresponding campus’ buildings but after choosing campus when I choose building, firebug shows nothing.

actionCreate method:




	public function actionCreate()

	{

		$model=new Event;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Event']))

		{

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

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}



I solved the solution with modifying actionCreate method, thanks both of you.

Good for you if you can solve it ;).

Can you post your solution so it is maybe useful for other users with a similar problem, thanks.

Since Events table holds only locationId(not building and campus), I had to fetch the locationId using campus and building. So I added this code into actionCreate:




$locationId = Location::model()->find('campus=:campus AND building=:building',

				array(':campus'=> $_POST['Location']['campus'], ':building'=> $_POST['Location']['building']));

				$locationId = $locationId['id'];

				$datas['locationId'] = $locationId;




Hi,

I am having a very similar issue so I figured I would post in here rather then start a new topic. I have a Subject and Category model. The subject has a foreign key to ‘id’ in the Category table. Currently I use a drop down in my create view that gets the category id and display the name of the category like this:




<?php echo $form->dropDownList($model,'category', CHtml::listData(Category::model()->findAll(), 'id', 'category'), array('empty'=>' ')); ?>



However when I go to save this I get an error:

I was trying (unsuccessfully) to get the id and assign it to the category_id field during the actionCreate. Is there an easy way to do this? Or would I have to make a sql call from within actionCreate to save this correctly?

Here is my actionCreate in SubjectController:


	public function actionCreate()

	{

		$model=new Subject;

							

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Subject']))

		{

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

						

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}

Any input would be appreciated. Thanks,

Dan