Issues

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

Issue Type: Bug Created: 2008-02-22T08:13:51.000+0000 Last Updated: 2008-03-31T16:04:35.000+0000 Status: Resolved Fix version(s): Reporter: Sebastian (basty) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:

Description

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

<pre class="highlight">
$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:

<pre class="highlight">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:

<pre class="highlight">      ...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):

<pre class="highlight">        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

Posted by Matthew Weier O'Phinney (matthew) on 2008-02-22T08:35:18.000+0000

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

<pre class="highlight">
$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.

Posted by Wil Sinclair (wil) on 2008-03-31T16:04:35.000+0000

Please evaluate and categorize as necessary.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts