3

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.

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I've seen this in the issue tracker, in fact, I'm the last one to comment and vote on this issue, however I don't like their workaround because I would need to instantiate a new instance of Zend_Form_Element_Select
@rockstarz all you should have to do in most cases is to pass the array to setOptions(): $element->setOptions(array('multiple' => false);
@RockyFord, that was it! Thanks so much. If you can post that as an answer I will mark it as correct
3

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.