dropDownList value not being saved

I am using Yii 1.1.9 and have spent some time looking at different forum posts and reading the available documentation but I guess I am still missing something. At this point some explanation of what I am missing would be greatly appreciated.

Problem:

My ActiveForm Contact has a Country dropDownList that is populated using a separate table and model, but the value is to be stored in my Contact table which has a country column.

Tables:

Contact cols(contact_id pk, firstname, lastname, etc., country)

Country cols(code pk, name)

Model is as follows:




<?php




class Contact extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Contact the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'contact';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('title, firstname, lastname, address, city, state, zip, country, citizenship, phone, email, birthdate, password', 'required'),

			//array('contact_id', 'numerical', 'integerOnly'=>true),

			array('title, state, zip, phone, birthdate', 'length', 'max'=>20),

			array('firstname, lastname, citizenship, email, password', 'length', 'max'=>50),

			array('address, country', 'length', 'max'=>150),

			array('city', 'length', 'max'=>100),

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

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

			array('contact_id, title, firstname, lastname, address, city, state, zip, country, citizenship, phone, email, birthdate, password', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'state' => array(self::BELONGS_TO, 'State', 'state_id'),

			'country' => array(self::BELONGS_TO, 'Country', 'country_code'),

		);

	}


	

	

}




Here is my view from _form.php:




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

	'id'=>'contact-form',

	'enableAjaxValidation'=>false,

)); ?>

...

<?php 

echo $form->dropDownList(new Country,'name', 

CHtml::listData(Country::model()->findAll(array('order' => 'name')), 'name', 'name'), 

array('empty'=> '--please select--'));

?>



This loads the countries into the dropDownList and sets the empty value as the default selection, however when I choose a different country submit it to the Controller to create the contact record the save fails, and the validation says ‘country cannot be blank’. In addition I noticed that the country I selected is not remembered by the form and the dropDownList returns to the default selected option. If I use a static array for the options the selection is remembered by the form.

Your help is greatly appreciated. :)

For completeness here is my actionCreate method in the Controller:




public function actionCreate()

	{

		$model=new Contact;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			$model->state=$_POST['state']; 

			$model->country=$_POST['country'];

			

		}

   ...




Hi

make one functions on model file

you have to generate array like below

array([key1] => value1

  [key2] =&gt; value2

)

so here you have to

function functionsNeme(){

$sql = (seleect * from table;

foreach($fechreroc as $value){

 &#036;array['option_value_field']

}

return $array

}

This is wrong.


echo $form->dropDownList(new Country,'name', 

CHtml::listData(Country::model()->findAll(array('order' => 'name')), 'name', 'name'), 

array('empty'=> '--please select--'));

Change to:




echo $form->dropDownList($model,'country', 

CHtml::listData(Country::model()->findAll(array('order' => 'name')), 'name', 'name'), 

array('empty'=> '--please select--'));



In the controller:




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

                {

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

                        //$model->state=$_POST['state']; not necessary

                        //$model->country=$_POST['country']; not necessary

                        

                }



Also, you aren’t calling $model->save() anywhere. Maybe you omitted that on purpose.

Normally your code will cascade like this:




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

{

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

  //$model->state=$_POST['state']; not necessary

  //$model->country=$_POST['country']; not necessary

                        

  if($model->save())

  {

  //happy dance

  } else 

  throw new CHttpException(500, 'Something went wrong');

  {


  }

}




Thanks. The problem was that I was getting the dropdown values from a reference model and trying to save them to it instead of the one I was declaring in the controller action for the form I was rendering. Oops! :D

Thanks man, that’s a lot of help :)

“Happy Dance”? Gotta Love a programmer with a sense of humor B)