ZF-11812: Potential bug with Zend_View_Helper_FormSelect

Issue Type: Bug Created: 2011-10-11T10:16:16.000+0000 Last Updated: 2012-07-20T19:16:55.000+0000 Status: Open Fix version(s): Reporter: Thomas Gray (base) Assignee: Pádraic Brady (padraic) Tags: - Zend_View

  • After1.12.0
  • state:patch-ready-for-review
  • zf-caretaker-adamlundrigan
  • zf-crteam-review

Related issues: - ZF-12094

Attachments: - ZF-11812.patch


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.


Posted by Adam Lundrigan (adamlundrigan) on 2011-10-28T17:47:41.000+0000

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:

<pre class="highlight">
php> var_dump(0 == 'adam');

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?

Posted by Rob Allen (rob) on 2012-06-13T20:41:16.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.