How to edit the default option in dropDownList?

I’m using a form generated by yii,it contain a field named ‘Country’ and i set it to be a dropdownlist,the dropdownlist works well.but there is a default option “Please select” by the first line of the options,I want to change the words,but I can’t find where to edit,I search the whole code source of yii and webroot,but find nothing,any one can help me ,thanks a lot!

add in your htmlOptions parameter the key=>value pair ‘prompt’=>‘Your message’,

Thanks for reply!

But I tried this before,it add another option ‘Your message’ before the ‘Please select’ option…

Here is the code I’m using now:




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

		<?php echo $form->dropDownList($model,'country_id',Lookup::country()); ?>

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



is this what you did?




<?php echo $form->dropDownList($model,'country_id',Lookup::country(), array('prompt'=>'Select a country:')); ?>



Yes!When I do this,I it work like this:




<select name="country_id]" id="country_id">

<option value="">Select a country:</option> 

<option value="0">Please select</option> 

...



That’s weird. I just tested the function and it works as expected




		$data = CHtml::listData(User::model()->findAll(),'id','username');

		

		echo CHtml::activeDropDownList(new UserProfile,'user_id',$data, array('prompt'=>'Select'));



produces




<select name="UserProfile[user_id]" id="UserProfile_user_id">

<option value="">Select</option>

</select>



maybe the point is "Lookup::country()" method?

:(

Post the code of that function

Try this


<?php echo $form->dropDownList($model,'country_id',Lookup::country(),array('empty' => 'Type what you need here',)); ?>

Hello,

I Have Table Country




CountryID   Country

1           China

2           Indonesia

3           USA



if there was empty(default) it will display CountryID =1




<select>

<option value="1" selected="selected">China</option>

<option value="2">Indonesia</option>

<option value="3">USA</option>

</select



how do i set in htmlOption "empty"?

Does my previous post not work for you?

Use




<?php echo $form->dropDownList($model,'country_id',$countries_array,array('empty' => '',)); ?>



This will produce the html code like




<select>

<option value=""></option>

<option value="1">China</option>

<option value="2">Indonesia</option>

<option value="3">USA</option>

</select>



Hello viter, thnks for your replay…

i didnt mean default value if model is empty , but default value Which custom choose…

I solve this setting value $model->CountryID = 1;

thanks for your help again

Well, for other people who may also been looking for this solution, there are several methods to solve this:

  1. The one that ariefpriyadi used, but it has the problem of not being the best solution if the dropdownlist is multiselect. Besides, it is a model level solution for an interface issue: unless we are talking about a very sensitive decision, sometimes the default-selection is a matter of visualization only, sometimes it is a programmer’s whim to leave a default selection, but it doesn’t deeply affect the database, nor the model itself, nor the application gravely, so I think that the solution should be in the views layer.

  2. Creating a default value in the table, is another solution that is being proposed in other threads.

This is worse, I think: it uses a database-level solution for something that is a matter of visualization (read the argument of point 1).

  1. Another solution is using the "options" entry in the array of htmloptions, I read it in the CHtml Dropdownlist (http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail) documentation, but I applied it for an ActiveForm Dropdownlist. So in this example, if the reader wants the item with key value "3" tobe selected by default, she/he should use:

<?php echo $form->dropDownList($model,‘country_id’,$countries_array,array(‘empty’ => ‘’,

‘options’=>array(3=>array(‘selected’=>‘selected’))

));

Best Regards, and let me congratulate once more the founders of this absolutely great framework. Is there a way to make a monetary donation to this project? I’ve been earning money with it…

Bogotá, Colombia

thanks. it works as expected

your analysis which is, i point to the arg(1) is the best solution regarding to this post.

congrats man…

<?php echo $form->dropDownList($model,‘country_id’,$countries_array,array(‘empty’ => ‘’,

‘options’=>array(3=>array(‘selected’=>‘selected’))

));

Seems tath this doesn’t work after data are POST with different countries value than default?

Hello Guys,

<?php echo $form->dropDownListrow($model, ‘parent_id’, CHtml::listData(Brand::model()->findAll(‘parent_id=0’), ‘id’, ‘name’)); ?>

I want to set Default Value in above list i.e Parent with value 0 But I have defined 0 value in table for parent_id when i am using array(‘0’=>‘Parent’) it inserting nothing in table means not set. EVEN If array(‘empty’=>‘Parent’) also shows error of not inserting empty value to DB , keeping parent_id in rules() safe also Not working…

Please Any solution Brothers?

Hi,

thanks to alexperimento, his solution 3) works a treat. I’ve used it with giix for a simple model to provide a no-set option with value 0, as this:


$form->dropDownList($model, 'stopby_1', GxHtml::listDataEx(Airport::model()->findAllAttributes(null, true)), array('empty' => '', 'options' => array(0 => array('selected' => 'selected'))));

Thanks!

rash*

hi all could you follow this dropdownlist this is for stable value for after the value error…

controller:




$data=Salary::model()->findAll('dept_id=:dept_id',array(':dept_id'=>(int) $_POST["User"]["dept_id"]));

                echo CHtml::tag('option',array('value' => ''),CHtml::encode('Select Salary'),true);

                echo CHtml::tag('option',array('value' => $data[0]["id"]),$data[0]["salary"],true);



view:




<?php

                    echo $form->dropDownList($model,'salary_id', !count($_POST) ? array(null): CHtml::listData(Salary::model()->findAll(array('order' => 'id ASC')), 'id', 'salary'), array('empty'=>'Select Salary'));

                ?>



With CHtml…


<?php echo CHtml::dropDownList('store', 'storeId', CHtml::listData(Sitestore::model()->findAll(),'storeId', 'storeName'), array('prompt'=>'Select Store')); ?>