hi all

hi i am working on a Dependent Dropdown and I am following this

unfortunately, i encounter the error [color="#FF0000"]“Object of class victim could not be converted to string”[/color]. Please help :-[

here are my _form and my controller code

_form




<?php

			$provId = CHtml::activeId($model, 'province_id');

			echo CHtml::dropDownList($model,'region_id', $region,

			array(

				'empty' => '',

				'style'=>'background-color:#FFFFFF',

				'ajax' => array(

					'type'=>'POST', 

					'url'=>CController::createUrl('dynamicprovinces'), 

					'update'=> '#'.$provId,

					),)); 

			echo $form->dropDownList('province_id','province_id', array(),array('empty' => '', 'class'=>'input input_r', 'style'=>'background-color:#FFFFFF'));

		?>

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



my controller




public function actionDynamicprovinces()

		{

		$data=Province::model()->findAll('region_id=:region_id', array(':region_id'=>(int)$_POST['region_id']));

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

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

		      {

		          echo CHtml::tag('option',

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

		      }

		}



thank you so much for your help! more power to yii :)

Strange… I don’t see in your code “victim”… do you have that class?

The problem is that you are using objects, not strings.

Use $value->field instead:


public function actionDynamicprovinces()

                {

                $data=Province::model()->findAll('region_id=:region_id', array(':region_id'=>(int)$_POST['region_id']));

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

                      foreach($data as $value)

                      {

                          echo CHtml::tag('option',

                          array('value'=>$value->id),CHtml::encode($value->province_name),true);

                      }

                }

thanks for your quick replies guys.

@jacmoe - i tried that and it didn’t work…

@norman

In future pleas give a meaning title to the post… "hi all" does not say anything at all about the post problem…

Maybe php is being sarcastic? :lol:

Norman: Define ‘doesn’t work’ - still the same error?

Shouldn’t be.

i’m sorry. it’s my first time here.

yeah it gives me the same error. i suspect that it has to do with the _form code because when i remove it, the application runs. :)

You are getting the message Object of class victim could not be converted to string

Do you have a class called victim? I don’t see it in your code…

And check the dropdown call you are sending ‘province_id’ two times…

sir pardon me but what do you mean when asking me do i have a class called victim? :<

That’s what your error message say -“Object of class victim could not be converted to string”

problem solved thanks

Good for you… but we would like to know what was the problem… and how it was solved…

Maybe in the future someone will have a similar problem and will find helpful your post with the solution…

Don’t do that to us! We are curious! :lol:

What was it?

heres what i did. i just redid my code earlier. i thought it was cluttered.

the _form snippet




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

		<?php echo $form->dropDownList($model, 'region_id', CHtml::listData(

		 Region::model()->findAll(), 'id', 'region_name'),

		 array(	

		 'prompt' => 'Select a Region',

		 'ajax' => array(

			 'type'=>'POST',

			 'url'=>CController::createUrl('victim/dynamicprovince'),

			 'update'=>'#'.CHtml::activeId($model,'province_id'),

		 ))); ?>



and on my VictimController,

i’ve included dynamicprovince on the accessRules() where it is allowed to create, update

heres the function dynamicprovince





 public function actionDynamicprovince()

		 {

			$data=Province::model()->findAll('region_id=:region_id',

			array(':region_id'=>(int) $_POST['victim']['region_id']));

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

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

			{

			 echo CHtml::tag('option',

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

			}

			

		 }



i’ve also set the relationships of each model.

i think the problem earlier was the line


    echo CHtml::dropDownList($model,'region_id', $region 

i think that $model shouldn’t be there :)

thanks guys for the support!

Thanks! :)

Yeah, you’re right: model should be in an active dropdown, not a html dropdown.