Dropdown, Radiobutton And Checkbox

Hi,

I’m new on yii developpement and I have a problem to understand the mechanism of these 3 forms (dropDown,radioButton,checkbox)

When we use activeDropDown or dropDownList I don’t understand ?

And I have a little problem displaying them.

I have 2 tables :

tbl_element (HAS_MANY value)

id

type

label

tbl_value (BELONGS_TO element)

id

value

idElement

With that I cannot create a radioButton nor dropdown nor checkbox:

here the code I tried to create:




<?php foreach ($elements as $element): 

switch($element->type){

   case 0: echo CHtml::textField("tf_$element->id");break;

   case 1: echo CHtml::textArea("ta_$element->id");break;

   case 2: echo CHtml::checkBoxList("values", <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />, <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />);

   case 3: echo CHtml::radioButtonList(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />,<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />,<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />);

   case 4: echo CHtml::dropDownList(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />,??,??);

}

 endforeach; ?>




Could someone help me to fill the question mark :)

Thanks

Hi,

In your scenario you have to use as specified below




When we use activeDropDown or dropDownList I don't understand ?


Use dropDownList. as you are trying to create the input fields manually not by database column. activeDropDown list is used if you are creating input field for database column. Remember active is meant to for databasecolumn  attribute and then the value of that column.



For more info on how to use the other fields check the below link:

http://www.yiiframework.com/doc/api/1.1/CHtml/

So I manage to make it work but it is the "best" solution ?


<?php foreach ($elements as $element): 

switch($element->type){

   case 0: echo CHtml::textField("tf_$element->id");break;

   case 1: echo CHtml::textArea("ta_$element->id");break;

   case 2: echo CHtml::checkBoxList("ck_$element->id", '', CHtml::listData($element->values,'id','value'));

   case 3: echo CHtml::radioButtonList("ra_$element->id", '',CHtml::listData($element->values,'id','value'));

   case 4: echo CHtml::dropDownList("dl_$element->id", '',CHtml::listData($element->values,'id','value'));

}

 endforeach; ?>

Thanks for help :D

Hi,

Use like this


<?php foreach ($elements as $element): 

switch($element->type){

   case 0: echo CHtml::textField("tf_".$element->id);break;

   case 1: echo CHtml::textArea("ta_".$element->id);break;

   case 2: echo CHtml::checkBoxList("ck_".$element->id, '', CHtml::listData($element->values,'id','value'));

   case 3: echo CHtml::radioButtonList("ra_".$element->id, '',CHtml::listData($element->values,'id','value'));

   case 4: echo CHtml::dropDownList("dl_".$element->id, '',CHtml::listData($element->values,'id','value'));

}

 endforeach; ?>

Based on your scenario for generating dynamic form fields or input fields it is the way to create but if you want to display the name for the input field you should also use CHtml::label($element->label); before switch statement.

hi mbagiella, you can use activeDropDownList() when you are working with model and it manages the validation errors and show the errorCss while dropDownList() cannot…

Yeah ok and what should I change ?

like this ?




<?php foreach ($elements as $element): 

switch($element->type){

   case 0: echo CHtml::activeTextField("tf_".$element->id);break;

   case 1: echo CHtml::activeTextArea("ta_".$element->id);break;

   case 2: echo CHtml::activeCheckBoxList("ck_".$element->id, '', CHtml::listData($element->values,'id','value'));

   case 3: echo CHtml::activeRadioButtonList("ra_".$element->id, '',CHtml::listData($element->values,'id','value'));

   case 4: echo CHtml::activeDropDownList("dl_".$element->id, '',CHtml::listData($element->values,'id','value'));

}

 endforeach; ?>



Thanks you advice

am not sure about this use like this

CHtml::activeTextField($modelName,‘attribute’);

CHtml::activeTextArea($modelName,‘attribute’);

CHtml::activeDropDownList($modelName,‘attribute’,$value);

refer guide for more reference