9

I have a dropdownlist in my _form model and I want to add a empty value (which I want as default). I have the following:

In _form:

<?php echo $form->labelEx($model,'country_id'); ?>
<?php echo $form->dropDownList($model,'country_id',Country::items(),array('empty' => '--Select a country--')); ?>
<?php echo $form->error($model,'country_id'); ?>

In Model Country:

public static function items()
{
 return CHtml::listData(Country::model()->findAllBySql(
 'SELECT * from country'), 
 'id', 'name');
}

Even my empty option is in the first row in dropdownlist, the 1st country in list shows as default.

I tried:

<?php echo $form->dropDownList($model,'country_id',
 Country::items(),array('empty'=>'--Select a country--',
 'options'=>
 array(
 '3'=>array('selected'=>'selected')
 )
 )); 
?>
 

In this way I can choose the default option, but cant set it to empty value, just countries that came from model:items.

Any idea?

Jason Aller
3,66028 gold badges43 silver badges40 bronze badges
asked Apr 17, 2013 at 10:27

5 Answers 5

22

Are you sure that country_id property of your model is not set to anything when you print the dropdown list? The following works for me if $model instance is created using new Country() operator but not by populating properties from database:

<?php echo $form->dropDownList(
 $model,
 'country_id',
 Country::items(),
 array(
 'empty'=>'--Select a country--')
 );
?>
answered Apr 17, 2013 at 11:08
Sign up to request clarification or add additional context in comments.

2 Comments

country_id is not setting anything. This $model is another model that has country_id as a FK. 'country' => array(self::BELONGS_TO, 'Country', 'country_id'), and in country model: 'team'=>array(self::HAS_MANY,'Team','country_id') ... I have this same code in another apps, and it works.. just in thsi case, the same code.. dont works...
Sorry, my BIG mistake, i set in database that country_id in tbl_team has a default value = 1...
12

Read documentation. There is 'prompt' parameter.

Try this:

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

See more details here http://www.yiiframework.com/forum/index.php/topic/11195-how-to-edit-the-default-option-in-dropdownlist/

answered Apr 17, 2013 at 11:13

Comments

1

You always can do something like array_merge in your items method

public static function items()
{
return array_merge(array(''=>'--Select a country--'), CHtml::listData(Country::model()->findAllBySql(
 'SELECT * from country'), 
 'id', 'name'));
}
answered Apr 17, 2013 at 11:24

Comments

1

I believe youre looking for:

echo $form->dropDownList($model,'country_id',Country::items(),array('prompt'=>''));
answered Apr 17, 2013 at 11:30

Comments

0

if you use yiibooster maybe this will help

<?php echo $form->dropDownListGroup(
 $model,
 'kode_cuti_sub2',
 array(
 'empty'=>'--Select a country--',
 'widgetOptions' => array(
 'data' => array('Something ...', 'Pilih Jenis Cuti'=>Chtml::listData(Cuti::model()->cuti_sub2(),'kode','jenis_cuti')),
 'options' => array(
 'placeholder' => 'Pilih NIP Pegawai',
 'width' => '100%',
 ),
 ),
 'wrapperHtmlOptions' => array(
 'class' => 'col-sm-5',
 ),
 )
 ); ?>

in my case it's worked

answered Jun 26, 2015 at 2:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.