CHtml Dropdownlist

Using CHtml class we are creating dropDownList in yii framework. This post will help you to understand about how to create dropDownList in different method, How to assign value, how to get dropDownList value in controller, dropDownList value from database, dropDownList validation in model yii framework.

Default Empty

Empty string, specifies the text corresponding to empty selection. Its value is empty. The ’empty’ option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning. If you set required in model and selected empty value from dropdownlist, You will get “required” error on form.

Using below code, we can create dropDownList in different way. And i added dropDownList value from database and assigned static array.

activeDropDownList With CHtml

CHtml activeDropDownList generates a dropDownList for a model attribute in yii framework.
Syntax:

public static string activeDropDownList(CModel $model,
                         string $attribute, 
                         array $data, 
                         array $htmlOptions=array ( ))                         

<?php  
echo CHtml::activeDropDownList($model,
                                'name',
                                array('1'=>'One','2'=>'Two'),
                                array('empty'=>'Select Option'));
//OR 
$type_list=CHtml::listData(Types::model()->findAll(),'id','type');
echo CHtml::activeDropDownList($model,
                                'name',
                                $type_list,
                                array('empty'=>'Select Option')); 

?>

dropDownList With Form

Syntax:

public static string dropDownList(CModel $model,
                         string $attribute, 
                         array $data, 
                         array $htmlOptions=array ( ))                         

<?php 
echo $form->dropDownList($model,
                        'name',
                        array('1'=>'One','2'=>'Two'),
                        array('empty'=>'Select Option'));
//OR 
$type_list=CHtml::listData(Types::model()->findAll(),'id','type');
echo CHtml::activeDropDownList($model,
                                'name',
                                $type_list,
                                array('empty'=>'Select Option'));  
?>

dropDownList With CHtml (Without Model Name)

Syntax:

public static string dropDownList(string name, string select,array data, array())
<?php     
echo CHtml::dropDownList('Typelist[name]',
                        2,
                        array('1'=>'One','2'=>'Two'),
                        array('empty'=>'Select Option'));
//OR
$type_list=CHtml::listData(Types::model()->findAll(),'id','type');
echo CHtml::dropDownList('Typelist[name]',
                            $selected_value='1',
                            $type_list,
                            array('empty'=>'Select Option'));
?> 

OUTPUT HTML

<select id="Typelist_name" name="Typelist[name]">
<option value="">Select Option</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<?php
.........
$model->attributes=$_POST['Typelist'];
echo $model->name;
.......
?>

I wrote the code in registerscript for dropdownlist change event.

<?php
Yii::app()->clientScript->registerScript('ddl-on-change','
    $("#Typelist_name").on("change",function(){
        var value = this.val();
        alert("DDL value"+value);
        //statement    
    });
');
?>


Read More About DropDownList And DropDownList Ajax