ZF-8452: Zend Form Element Select Implied Multiple Select

Description

When using Zend_Form_Element_Select in conjunction of setIsArray it is implied that you want a multiple select, when you may not. Now this is definitely a feature for those who want it, but for those who want multiple selects it's a problem.

For example if I use the following code:


$field = new Zend_Form_Element_Select('field');
$field->setIsArray(true)
      ->setMultiOptions(array(
        'a' => 'A',
        'b' => 'B',
        'c' => 'C',
      ));

I get the following output:


ABC

When I want the desired output:


ABC

I have looked into work arounds for this problem, but I don't feel they're appropriate for such a simple problem which can easily be corrected by removal of three lines.

Now I propose either we remove the offending code which is on 77 to 79 of Zend_View_Helper_FormSelect which will simply mean people wanting multiple select will be required to add setAttrib('multiple', 'multiple') but will break backwards compatability I believe, or add an option to disable that code.

Comments

Yes, this is extremely strange behaviour; I have been trying to figure out why it is doing this, so thanks for the bug report to know that I'm not going mad. I have no idea how to build a normalselect box in Zend forms, will post here when I figure it out. Seems like creating a normal, simple select box should be simpler, but then that could be said about so much of zend.

Please fix.

A ZFForums article describes a temporary fix for this issue.


<?php
    $element = new Zend_Form_Element_Select('selectbox', array('multiple' => false ));
    $element->setIsArray(true);

I agree this it a temporary fix, however, there are circumstances where this wouldn't be an option (creating a new instance). One should be able to getElement and add this setIsArray attribute and NOT automatically turn your select into a multi select. This is oversight at best.

I should also mention this issue stil exists in Zf 1.12.1 many years after this issue was reported.

I will look into it.

Here's the work around:

$element = $this->getElement('element'); $element->setMultiOptions(array('a' => 'A')) ->setIsArray(true) ->setOptions(array('multiple' => false);

@Jonathan Look at the comment from Justin Jahn. ;)

Patch with fix and unit test added. (includes changes from {{assertType}} to {{assertTrue}})

Fixed on trunk (25182) and release-1.12 (25183)