Yii 1.1: Drop down list with enum values for column of type ENUM >+> incorporate into giix


Let's say our table 'mug' has a column named 'color' of the type ENUM('red','green','blue').
We want to replace the textfield for the attribute color in the create and update forms of a 'mug' with a drop down list, which has the enum values as options.
The main code was contributed by zaccaria in the forum (see this post).

I merely repeat that useful peace of code here, so it is available in the wikis, and describe how you can incorporate it into giix, so the drop down list is added automatically during code generation.

The enumDropDownList

Create the file ..\components\ZHtml.php with the following code:

class ZHtml extends CHtml
    public static function enumDropDownList($model, $attribute, $htmlOptions=array())
      return CHtml::activeDropDownList( $model, $attribute, self::enumItem($model,  $attribute), $htmlOptions);
    public static function enumItem($model,$attribute) {
        foreach(explode("','", $matches[1]) as $value) {
        return $values;

Then use it in the view files with:

<?php echo ZHtml::enumDropDownList( $model,'color' ); ?>

Adding it to giix' CrudCode

If you use giix for code generation, you can put the code above into the file ..\extensions\giix-components\GxHtml.php (only the function definitions, not the 'class' part).

Then add the following code into the file ..\extensions\giix-components\GxActiveForm.php:

public function enumDropDownList($model, $attribute, $htmlOptions = array()){
        return GxHtml::enumDropDownList($model, $attribute, $htmlOptions);

Now you can use it in your view files with:

<?php echo $form->enumDropDownList($model, 'color')); ?>

To edit giix so it adds this line into the view files during code generation: in the file ..\extensions\giix-core\giixCrud\GiixCrudCode.php find the function generateActiveField. Add the condition for the enum type (enclosed between /*NEW_BEGIN* and /*NEW_END*/) into the code, so it looks like:

if (strtoupper($column->dbType) == 'TINYINT(1)'
        || strtoupper($column->dbType) == 'BIT'
        || strtoupper($column->dbType) == 'BOOL'
        || strtoupper($column->dbType) == 'BOOLEAN') {
    return "echo \$form->checkBox(\$model, '{$column->name}')";
else if (stripos($column->dbType, 'enum') !== false) {
    return "echo \$form->enumDropDownList(\$model, '{$column->name}')";
} /*NEW_END*/
else if (strtoupper($column->dbType) == 'DATE') {

That's it. If something is unclear/wrong/incomplete or you have other suggestions please let me know.

Total 4 comments

#19970 report it
Rahmad Subekti at 2016/10/31 11:06pm
Fail to try it

Sorry if I so late to read it and give the comment. But I hope anyone could help me to fix my problem. I downloaded giix extension. I have followed the tutorial above. But I don't know why it doesn't work. When I compiled it, the browser display CException that said : CActiveForm and its behaviors do not have a method or closure named "enumDropDownList". Please, anyone could help me ? because this project very important to me. Thank you

#18121 report it
markringo at 2014/09/12 06:17am
it helped me

wow, this code works like magic. Thanks!

#8325 report it
pckabeer at 2012/05/25 04:34pm
RadioButtonlist Version of this code

The RadioButtonlist version of this code enumRadioButtonlist

#6939 report it
yiqing95 at 2012/02/14 09:43pm
useful solutions , well done

although this solution have existed two years . it 's still helpful to us .

Leave a comment

Please to leave your comment.

Write new article