0

In my project, I am required to have many drop-down fields with array-notated names, like so:

<select name="language[]">...</select>
<select name="language[]">...</select>
<select name="language[]">...</select>

Since I need to render Zend's formSelect view helper for it, I thought it was as simple as doing:

for($i = 0; $i < 3; $++){
    echo $this->formSelect('language[]', $this->languages[$i],
                                         $this->attribs[$i],
                                         $this->languageOptions[$i]);
}

But I was wrong: Instead of rendering them as regular drop-down fields, Zend decides to be a smarty-pant and render them as multiple-select fields, instead.

I understand the thinking behind this behavior, but it just so happens that it the help formSelect provides is too much for my needs.

Now, I can amend it with using JavaScript to remove the multiple="multiple" attribute to transform them to regular drop-downs...

But out of curiosity, is there a way to make formSelect render these elements as regular drop-down fields, other than modifying its underlying code?

1 Answer 1

1

You are correct, the formSelect helper is deciding to make it a multi-select based on the fact that the element name is an array[].

You can override the behavior however.

To do so, add multiple => false to your list of $attribs for the element.

E.g.

$attribs = array('multiple' => false, 'class' => 'selection');
echo $this->formSelect('not_array[]', '', $attribs, $options);

This should prevent it from trying to make the element support more than one selection.

Sign up to request clarification or add additional context in comments.

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.