Error Creating Dropdownlist

Hi,

I’d like to create a dropdown list for _form which is populated from an enum field in my table. I found the following link ‘http://www.yiiframework.com/wiki/303/drop-down-list-with-enum-values-for-column-of-type-enum-incorporate-into-giix#hh0’ and created …\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) {


    $attr=$attribute;


    self::resolveName($model,$attr);


    preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);


    foreach(explode(',', $matches[1]) as $value) {


            $value=str_replace("'",null,$value);


            $values[$value]=Yii::t('enumItem',$value);


    }


    return $values;


} 

}

Then I used it in _form, replacing the default with:

<?php echo $form->labelEx($model,‘prefix’); ?>

<?php // echo $form->textField($model,‘prefix’,array(‘size’=>4,‘maxlength’=>4)); ?>

<?php echo ZHtml::enumDropDownList( $model,‘prefix’ ); ?>

<?php echo $form->error($model,‘prefix’); ?>

My table enumYype field is named ‘prefix’ and is populated with ‘enum(‘Mr’, ‘Mrs’, ‘Ms’, ‘Dr’, ‘Rev’)’

My problem is that _form gives me ‘Fatal error: Class ‘ZHtml’ not found in C:\wamp\www\laup\protected\views\tblContacts\_form.php on line 62’. On the same error page, under the Call Stack Function column, I also get the message ‘Fatal error: Class ‘ZHtml’ not found in C:\wamp\www\laup\protected\views\tblContacts\_form.php on line 62’ and ‘require( ‘C:\wamp\www\laup\protected\views\tblContacts\_form.php’ )’. Both have ‘…\CBaseController.php:126’ undet the Call Stack Location column.

My best guess as a Newbie is that I’m not pointing to the Class properly, or I may need an addition line of code to tie all ths together, but don’t know where it goes. …maybe in a Controller??

Can anyone help with direction on this?

Hi ronallensmith

Can you use the code tag (the one that looks like <> on the editor menu) to format any code you are posting, please? It make code it easier to read.

The error means your form can’t find the class. In your main.config file, do you have the application.components.* in your import array?




'import'=>array(

       .....

        'application.components.*',//<-this one

.....

),



Also make sure the file name matches the name of the class. Should be ‘ZHtml.php’.

If that doesn’t work, read this thread.

Edit:

One more thing, does your code have the <?php ?> tags?

Hi, thanks. Sorry about the formatting. My main.php has:




return array(

        ...


	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),


        ...	


);



My _form.php has:


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

where


<?php echo $form->textField($model,'prefix',array('size'=>4,'maxlength'=>4)); ?>

used to be.

And …‘protected\components’ has ZHtml.php file.

I also ran across the post you pointed me too before. I’ll take a closer look.

–The following works–

Added the following to the Model:




	public static function enumItem($model,$attribute)

        {

                $attr=$attribute;

                // self::resolveName($model,$attr);

                preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);

                foreach(explode(',', $matches[1]) as $value)

                {

                        $value=str_replace("'",null,$value);

                        $values[$value]=Yii::t('enumItem',$value);

                }

                

                return $values;

        }



Added the following to View _form.php:




<?php echo CHtml::activeDropDownList( $model,'prefix',$model->enumItem($model, 'prefix') ); ?>



At first glance, the post you pointed me to did not not strike me as significant in that this route applys directly to the Model and not a Class–completely went over my head the first time around, until you suggested a re-visit. Thanks for furthering my education! :)

I’m glad it’s sorted. Happy Yii-ing :)