ZF-2711: Can´t add options to a selectbox with a config array to Zend_Form::_construct()

Description

If you try to define a form by an array, witch should contain a selectbox, you can´t add options to it.


$config = array(
  'action' => '/zftest/data/save',
  'method' => 'post',
  'elements' => array(
    'idlanguage' => array(
      'select',
      array(
        'label' => 'Language',
        'attribs' => array('readonly' => 'readonly',
          'options' => array(1 => 'test1', 2 => 'blabla2'),
        ),
        'validators' => array(array($int, 'notInt')),
        'required' => TRUE
      ),
    )
  )
);
$form = new Zend_Form($config);

This gets the following HTML-Code:

Array

The mistake is on /Zend/Form/Element.php in the method setOptions(array $options). foreach($options as $key => $value) { ... } If $value contains a array(), this will be converted to a string. So $value contains "Array".

I thought, when 'attribs' can´t contain an array, i will defined the select-options one level higher:

      ...array(
        'label' => 'Language',
        'attribs' => array('readonly' => 'readonly'),
        'options' => array(1 => 'test1', 2 => 'blabla2'),
        'validators' => array(array($int, 'notInt')),
        'required' => TRUE
       ),...

But this also doesn´t work, because 'options' is a reserved word (i think):

        foreach ($options as $key => $value) {
            if (in_array($key, array('options', 'config'))) {
                continue;
            }

So, it is not possible for me to add options to the selectbox (or is there an other solution?)

Comments

Use setMultiOptions() to set options for a selectbox (or use the config option 'multiOptions'):


$element->setMultiOptions(array(1 => 'test1', 2 => 'blabbla2'));

// or:
$config = array(
    // ...
    'elements' => array(
        'idlanguage' => array(
            'select',
            array(
                'label' => 'Language',
                'attribs' => array(
                    'readonly' => 'readonly',
                ),
                'multiOptions' => array(
                    1 => 'test1',
                    2 => 'blabbla2',
                ),
                'required' => true
            )
        ),
    ),
    // ...
);
$form = new Zend_Form($config);

This will be documented properly in the coming days.

Please evaluate and categorize as necessary.