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
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'):
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.