Question

When using Zend_Form_Element_Select in conjunction with setIsArray for some reason Zend Framework 1.12 assumes that you want a multiple select. This seems like a strange behavior of the framework, so I'm think there must be a workaround or another option I'm just not setting.

For example if I use the following code:

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

I get the following output:

<select name="element[]" id="element" multiple="multiple">
<option value="a" label="A">A</option>
</select>

When I want the desired output:

<select name="element[]" id="element">
<option value="a" label="A">A</option>
</select>

i.e. I don't want multiple="multiple"

I have looked into work arounds for this problem, but I don't feel they're appropriate for such a simple problem like adding brackets to the name of your form element. At this point I'm thinking of using jquery to just remove this multiple attribute on page load, but that's really hacky and can't imagine Zend Framework would be working this way.

Does anyone know how to do this using the code example above? I don't want to instantiate a new instance of zend form select, or addElement because one was already established.

Was it helpful?

Solution 2

So this is the solution to this issue, using the setOptions method with multiple => false as shown below in an example:

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

OTHER TIPS

There is a very similar thread at ZF Issue tracker, did you try the suggested workaround?

$element = new Zend_Form_Element_Select('selectbox', array('multiple' => false ));
$element->setIsArray(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top