Dropdown Menu In Form And Manage View

Hi All,

I am new to YII and this forum. Before posting i have searched the forum but not got any helpful simple explanation on my problem.

I want to create a dropdown menu for my Status column which should display low,medium and high. After reading the cookbook i have done the below steps.

Below is the snip from _form.php

    <div class="row">


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


	<?php echo $form->dropDownList($model,'Status', $model->getTypeOptions()); ?>


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


</div>

Below snip form models\Master.php

class Master extends CActiveRecord

{

const TYPE_LOW=0;


const TYPE_MEDIUM=1;


const TYPE_HIGH=2;

public function getTypeOptions()

{


	return array(


		self::TYPE_LOW=>'Low',


		self::TYPE_MEDIUM=>'Medium',


		self::TYPE_HIGH=>'High',


		);


}

I am able to get the dropdown menu listing in my form but when i select Low and submit the form

Value inserted in mysql database is 0 and for medium it is 1 and for high it is 2.

Please help me i am stuck here.

Also i want the same to be listed in Manage view so that i can filter the data using dropdown option.

Thanks in advance.

Regards

Sanjay

Hi sanjay, and welcome to the forum.

There’s nothing wrong with that. It is just what is expected.

We use the integer constants (TYPE_LOW, TYPE_MEDIUM and TYPE_HIGH) for our data. We store them in db and we read them from db.

But we show the labels of them (‘Low’, ‘Medium’ and ‘High’) to the users.

A drop down list holds the pairs of value and text. So the users will select a text but we’ll get the corresponding value.

Hi softark,

Thanks for your quick response.

But i want the text (‘Low’, ‘Medium’ and ‘High’) to be stored in database.

Please let me know what modifications needs to be done to achive this.

Also i want the same to be listed in Manage view so that i can filter the data using dropdown option.

Regards

Sanjay

Define your constants as string:




	const TYPE_LOW='Low';

	const TYPE_MEDIUM='Medium';

	const TYPE_HIGH='High';



I don’t know what you mean by ‘Manage view’, but if you want a dropDownList filter in your CGridView, then:




'columns' => array(

    ....

    array(

        'name' => 'Status',

        'filter' => $model->getTypeOptions(),

    ),

    ....



Hi softark,

Thanks again for your quick help.

Earlier i have tested giving the constants as string but it was giving exception error at that time. I don’t know what went wrong at that time.

Now i have done as per your suggestion and it is working as expected and the values are going in database as string. Yesterday i wasted almost 4Hrs googling for this solutions. Thanks a lot for your help.

Yes i mean CGridView as i am new to YII so don’t know the term.

I have copied the entry in CGridView and it is working but now there are two columns showing of Status field,

the one which was earlier and the other one of dropdown after copying the code, so i commented the earlier entry in CGridView.

After commenting now it is showing only the dropdown one.

Let me know what i have done is correct or not. or should i delete the entry

Regards

Sanjay

I think it’s good. :)

As for the unnecessary column, you can comment it out or remove it completely. Do as you like. There’s not much difference between the two.