I have a custom form in my module (in admin). It has a select, whose default value should be "*", but instead, it has both "*" and "0" selected:

<select id="customer_group_id" name="customer_group_id" title="Customer Group" class=" select">
    <option value="*" selected="selected">ANY</option>
    <option value="0" selected="selected">NOT LOGGED IN</option>
    <option value="1">General</option>
    <option value="2">Wholesale</option>
    <option value="3">Retailer</option>
    <option value="4">Partners</option>

The field is being added to the form with this code:

$fieldset->addField('customer_group_id', 'select', array(
    'name'   => 'customer_group_id',
    'label'  => 'Customer Group',
    'title'  => 'Customer Group',
    'values' => $helper->getAvailableCustomerGroups(),
    'value'  => '*'

It gets its values from a function in my helper class. This function simply grabs all of the customer groups in the store, and also adds the "*" option.

function getAvailableCustomerGroups() {
    return array_merge(


Why does the form have TWO options marked SELECTED?

1 Answer 1


This is a bug deep within the Varien_Data_Form_Element_Select class. I was able to track it down by adding this debugging code to the _toOptionHtml() function in Varien_Data_Form_Element_Select:

if (in_array($option['value'], $selected)) {
    $type = gettype($option['value']);
    Mage::log("Marking option ($type){$option['value']} as SELECTED because it is in the values array.");

    $html.= ' selected="selected"';

This gives me a log like:

DEBUG (7): Element name: customer_group_id  Element value: *
DEBUG (7): Marking option (string)* as SELECTED because it is in the values array.
DEBUG (7): Array (
    [0] => *

DEBUG (7): Marking option (integer)0 as SELECTED because it is in the values array.
DEBUG (7): Array (
    [0] => *

I can now see what is happening; the "NOT LOGGED IN" group has a value of (integer)0, and not (string)0. This is a well known "got'cha" with the in_array function, as explained in this SO question: https://stackoverflow.com/a/13846839/884734

This makes sense, because toOptionHash() would return a key=>value type structure, and it's an (int) column in the database, so logically php must use (integer) for the index.

An easy work-around to this bug is to change getAvailableCustomerGroups() function to return an optionArray and not an optionHash. Here is the fixed helper code:

function getAvailableCustomerGroups() {
    return array_merge(
            array( 'value' => '*', 'label' => 'ANY' )

As for fixing the underlying bug, hopefully the core devs will.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.