Yii Framework Forum: ActiveDropDownList - Please select - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

ActiveDropDownList - Please select Want to add a "Please select" option Rate Topic: -----

#1 User is offline   mintao 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-December 09
  • Location:Munich, Germany

  Posted 21 January 2010 - 12:13 PM

Hi,

Don't know if it's an idea for a new feature, or if I only didn't find it in the documentation. I have 2 models in relation eg. User and Country. So in the rules of User-model i have "allowEmpty=>true". This means, the user CAN specify his country.

This is where my nightmare starts.

When creating an activeDropDownList like:

<?php
$options = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::activeDropDownList($model,'country_id', $options);
?>

the user has no possibility to leave this one out, because the first entry is always selected.


Then I had the Idea to prepend an empty array_element "array(''=>'Please select your country')" like this:

<?php
$select = array(''=>'Please select your country');
$options = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::activeDropDownList($model,'country_id', array_merge($select, $options));
?>


This time everything _looks_ good ... but isn't! Array_merge() throws away all array_keys (array_merge_recursive too). So all my IDs for the countries all get lost and first entry in the DropDown gets ID 0, second ID 1 and so on.

Then I had the idea to convert the ID to a string to prevent php from rewriting the keys:

<?php
// Country.php - The country model
[...]
    public function getStringId()
    {
        return (string) $this->id;
    }
?>


then justify the dropdown:

<?php
$select = array(''=>'Please select your country');
$options = CHtml::listData(Country::model()->findAll(), 'stringId', 'name');
echo CHtml::activeDropDownList($model,'country_id', array_merge($select, $options));
?>


... and guess what - this doesn't work either :( ... only by prepending a char PHP doesn't throw away my keys:

<?php
// Country.php - The country model
[...]
    public function getStringId()
    {
        return (string) 'X'.$this->id;
    }
?>


... but this time the populated data doesn't work and I also think there has to be a more gentle way to solve this. Am I right?

My idea for a feature would be to get this possibility to prepend a "Choose your ...." for dropdowns if empty values are allowed.

But never the less, I need a quicker solution than waiting for 1.2 ;)


Does anybody could help me out of this?

Thank you!
0

#2 User is offline   ujovlado 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 07-January 10
  • Location:Slovakia

Posted 21 January 2010 - 03:02 PM

i think you are too lazy to read documentation :P


you have two tables, Countries and Users

Countries has: country_id, country_name
User has: user_id, user_name, country_id


and here you can select a country when editing user information

echo CHtml::activeLabelEx($model,'country_id');
echo CHtml::activeDropDownList($model,'country_id', CHtml::listData(Countries::model()->findAll(), 'country_id', 'country_name'));



or if you select all countries and have an array of them
for example;
$countries = array(6 => 'Slovakia', 12 => 'Poland', 25 => 'United States');


you can add a "zero" country:
$countries[0] = "---";


and the view:
echo CHtml::activeLabelEx($model,'country_id');
echo CHtml::activeDropDownList($model,'country_id', $countries);



this is it:
http://www.yiiframew...DownList-detail


;)
When life gives you lemons, just shut up and eat your damn lemons ...
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 21 January 2010 - 03:50 PM

Please read http://www.yiiframew...DownList-detail
You can set 'empty' or 'prompt' option in $htmlOptions
0

#4 User is offline   ujovlado 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 07-January 10
  • Location:Slovakia

Posted 21 January 2010 - 03:55 PM

View Postqiang, on 21 January 2010 - 03:50 PM, said:

Please read http://www.yiiframew...DownList-detail
You can set 'empty' or 'prompt' option in $htmlOptions


yes, you're right :) and in activeListBoX too, becaues it's a special type of activeDropDownList.

but i want to show him another way too.

;)
When life gives you lemons, just shut up and eat your damn lemons ...
0

#5 User is offline   mintao 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-December 09
  • Location:Munich, Germany

Posted 21 January 2010 - 04:22 PM

Thank you funner,

You won't belive how much I'm reading all day/night long about yii, which fascinates me a lot. First time that I get first real life results within a few weeks. (OK, except of cake, which is too heavy and overloaded in my opinion).

Your way was not exactly the solution, because
  • the country id 0 is appended to the end of the list (I know there are ways to get it up) and
  • when trying to save country id 0 I get an exception from the DB that no foreign key could be found, so for this special case I'd need to write an beforeSave() "if field==country_id and id=0 then country=null"-function to set country id 0 to NULL.


But ... your answer pointed me into the right direction and now I have what I was looking for:
The htmlOption "empty". Just in case someone else is blind too - just like me:

<?php
$countries=CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::activeDropDownList($model,'type_id',$countries,array('empty'=>'Please select a country'));
?>


Thank you and happy coding
Florian
0

#6 User is offline   mintao 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-December 09
  • Location:Munich, Germany

Posted 21 January 2010 - 04:24 PM

oh ... waited too long with my post (had a phone call) :)

Thank you again!
0

#7 User is offline   ujovlado 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 07-January 10
  • Location:Slovakia

Posted 21 January 2010 - 04:31 PM

View Postmintao, on 21 January 2010 - 04:22 PM, said:

Thank you funner,

You won't belive how much I'm reading all day/night long about yii, which fascinates me a lot. First time that I get first real life results within a few weeks. (OK, except of cake, which is too heavy and overloaded in my opinion).

Your way was not exactly the solution, because
  • the country id 0 is appended to the end of the list (I know there are ways to get it up) and
  • when trying to save country id 0 I get an exception from the DB that no foreign key could be found, so for this special case I'd need to write an beforeSave() "if field==country_id and id=0 then country=null"-function to set country id 0 to NULL.


But ... your answer pointed me into the right direction and now I have what I was looking for:
The htmlOption "empty". Just in case someone else is blind too - just like me:

<?php
$countries=CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::activeDropDownList($model,'type_id',$countries,array('empty'=>'Please select a country'));
?>


Thank you and happy coding
Florian



there's no need to write beforeSave() method ... only if you have (and you have) an innodb table with foreign keys.

if you set the country_id in Users to integer e.g.
INT(10)
, set
DEFAULT NULL
and enable
NULL
everything will be ok :)

--

and what about the "zero" country at the end?

use asort() function in PHP:
asort($countries);

When life gives you lemons, just shut up and eat your damn lemons ...
0

#8 User is offline   Fixticks 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 31-May 11
  • Location:Uganda

Posted 11 March 2012 - 07:31 PM

i used this coz sometimes it would not select the 'Please Select' option as default.

echo CHtml::dropdownlist('countries','',CHtml::listData($countries,'id','name'), array('empty'=>array('0' => '--- Please Select ---')));

A glass of water and 2 spoons of glucose makes the bugs go away
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users