0

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>
</select>

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(
        array('*'=>'ANY'),
        Mage::getResourceModel('customer/group_collection')->toOptionHash()
    );

}

Why does the form have TWO options marked SELECTED?

0

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.");
    Mage::log($selected);

    $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(
            array( 'value' => '*', 'label' => 'ANY' )
        ),
        Mage::getResourceModel('customer/group_collection')->toOptionArray()
    );
}

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, privacy policy and cookie policy

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