Checkbox Options...

I have a role table: containing role_id and role.

Then I have a user table: containing user_id, name , pass and role id

When I create… The _form.php: I would like for the role id to be a dropdown menu and would display only the options in role.role_id…

How can I do this?

Dear Friend

Suppose you have a two tables.

1.role

id ### name

2.user

id ### name ### password ### role_id

First we have to declare a static function in Role model to fetch the role names in an array format.

Here array value would be role.name and array key would be role.id




class Role extends CActiveRecord

{


public static function fetchRoleNames()

   { $arr=array();

     $roles=Role::model()->findAll();

     foreach($roles as $role)

       {

           $arr[$role->id]=$role->name;

       }

     return $arr;

   }


}



Then in your views/user/_form.php, make following changes for role_id.




<div class="row">

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

<?php echo $form->dropDownList($model,'role_id',Role::fetchRoleNames()); ?>

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

</div>



I hope this is what you expected.

Regards.

why in the world you wanna write another method just to populate the drop down list when you do the following


	<div class="row">

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

			<?php echo $form->dropDownList($model,'role_id', CHtml::listData(Role::model()->findAll(), 'id', 'name')); ?>

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

		</div>

[color="#008000"] NOTE: moved to proper section (General Discussion for Yii 1.1.x instead of Tips, Snippets and Tutorials) [/color]

Hey, thanks to all the replies, got this topic RESOLVED. :)