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!