ZF-11812: Potential bug with Zend_View_Helper_FormSelect

Description

Hey ZF Team,

Think I have found a bug in Zend_View_Helper_FormSelect.

Example of this would be passing an array say a list of 0 to 5 as the value on the select element.

$multiItems = array( 'def' => 'Please select..', '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5' );

If I then created an element like this:

    $this->addElement('select', 'selectElement', array(
        'label' => 'Number list',
        'multiOptions' => $multiItems,
        'disable' => array('-'),
    ));

Then the first item (with the value of hyphen '-') will be disabled but so will the item with the value of 0.

After a little investigation this seems to be with the Zend_View_Helper_FormSelect in the _build method on line 172 of my version of the view helper you have:

if (in_array($value, $disable)) { $opt .= ' disabled="disabled"'; }

If I change this to:

    if (in_array($value, $disable, true)) {
        $opt .= ' disabled="disabled"';
    }

So that the strict option is enabled then it fixes the problem.

Not sure if this is a bug or a feature that I haven't seen docs for.

Comments

Confirmed. This happens due to "oddities" in how {{in_array}} handles zero values (Details here). By default, {{in_array}} uses a loose comparison (==), so if the array contains an integer zero value it will cause {{in_array}} to convert the needle to integer before comparing. Converting a character to an integer makes it zero, so any string will match that zero:


php> var_dump(0 == 'adam');
bool(true)

Additional to what you have suggested, it's also necessary to use {{strval}} to convert both the needle and haystack values to string representations to get consistent matching. I've attached a fix, complete with unit test, to this issue. Could you please try it out and see if it meets your needs?

After 1.12 as not sure what the implications of this change are.