DropDownList Data

Hi People.

I’m a noob in Yii, so i’m learning it yet.

I’m using the method CActiveForm::dropDownList() and having a problem to load data in the dropdownList.

In this function i get the data.




public function actionCreate()

	{

		$model=new Cidades;

                $mestados = new Estados();

                $estados = $mestados->getAllEstado()->queryAll();

                #var_dump($estados);exit;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

                        'estados'=>$estados,

		));

	}

With the var_dump in $estados…




array

  0 => 

    array

      'id_estado' => string '1' (length=1)

      'desc_estado' => string 'Minas Gerais' (length=12)

  1 => 

    array

      'id_estado' => string '2' (length=1)

      'desc_estado' => string 'São Paulo' (length=10)

  2 => 

    array

      'id_estado' => string '3' (length=1)

      'desc_estado' => string 'Rio de Janeiro' (length=14)

  3 => 

    array

      'id_estado' => string '4' (length=1)

      'desc_estado' => string 'Bahia' (length=5)




The code than i use the dropDownList is these:


<div class="row">

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

		<?php echo $form->dropDownList($estados,'desc_estado',array('size'=>10,'maxlength'=>10)); ?>

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

	</div>



But its happening this error when i try run it.


get_class() expects parameter 1 to be object, array given


D:\3NW\Dev\Yii\yii-1.1.8.r3324\framework\web\helpers\CHtml.php(2028)

Hope someone can help me.

Thanks.

Big Hugs

By a fast look I would say that this


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

should be


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

Thank’s for your help mdomba, but the error persists.

From what i saw, this field is just the label. I tried to change in others forms, and worked normally.

Hugs.

Hi,

DropDown list expects 3rd parameter to be an array with data , it can be an array $key => $value eg. array( 1 => ‘Minas Gerais’, 2 => ‘São Paulo’) or object of ListData http://www.yiiframework.com/doc/api/1.1/CHtml#listData-detail

Good luck with learnig Yii :)

Hi jagutex,

A few minutes ago, studying the documentation i had seen it.

Now its working normally. Thanks for the help.

abraço.