ZendFramework - How to create optgroup and there option using view helpers?
Asked Answered
L

2

12

How do i create this with $this->formSelect() ?

<select multiple>
    <optgroup label="a">
       <option>1</option> 
       <option>2</option>
    </optgroup>
    <optgroup label="b">
       <option>1</option> 
    </optgroup>
</select>
Lixivium answered 22/2, 2012 at 19:59 Comment(0)
P
12

For the Zend_Form_Element_Select() it goes like this

$multiOptions = array(
  'Group A' => array(1 => 'First Value',2 => 'Second Value A),
  'Group B' => array(3 => 'Third Value'),
);

$element->setMultiOptions($multiOptions);

Note that you also have addMultiOption($option,$value) and addMultiOptions($options). Simply include the value or options in an additional array.

Preter answered 22/2, 2012 at 23:28 Comment(4)
How can i have Group A also a ID? like 001Lixivium
#Google Not sure what you mean with ID. An optgroup is not a item you can select. In Zend the key for an array Group A becomes a label and the label looks like a title in the drop down menu. See W3SchoolsPreter
I am interested in the question being answered using XML, as it was requested, which would be useful with Zend_Config_XML and Zend_Form_ElementMarsupium
Please note that setMultiOptions() no longer exists in Zend Framework 3. Use Mr Coder's answer instead.Infarct
F
13

In Zend Framework 2 this can be done as follows:

$this->add(array(
        'name'=>'Test',
        'type'=>'Zend\Form\Element\Select',
        'attributes'=>array('type'=>'select','required'=>'required'),
        'options'=>array(
            'label'=>'Test',
            'value_options'=>array('fruits'=>array('label'=>'Fruits','options'=>array('1'=>'Apple','2'=>'Mango')),'animals'=>array('label'=>'Animals','options'=>array('cat'=>'CAT','dog'=>'DOG'))),
            'empty_option'=>'Please Select'
        ),

    ));

please note that an option named empty_options doesn't exist instead empty_option should be used.

Foretime answered 12/10, 2013 at 3:22 Comment(4)
@DrCord I don't think you should have accepted the suggested edit - it should have been a comment.Kinetics
maybe part of could have been a comment but the edit to the actual code that makes it working for users to copy paste as they are prone to do was a worthwhile edit.Joh
@Joh Even so, the first sentence shouldn't be here.Kinetics
I moved it to the end. I don't agree it should be removed, how does it hurt the answer? if the answer that was here was only a typo then this won't help anyone but if it was because of a bad piece of code somewhere out there that users are using to tutorial from or something then this will certainly help many people...Joh
P
12

For the Zend_Form_Element_Select() it goes like this

$multiOptions = array(
  'Group A' => array(1 => 'First Value',2 => 'Second Value A),
  'Group B' => array(3 => 'Third Value'),
);

$element->setMultiOptions($multiOptions);

Note that you also have addMultiOption($option,$value) and addMultiOptions($options). Simply include the value or options in an additional array.

Preter answered 22/2, 2012 at 23:28 Comment(4)
How can i have Group A also a ID? like 001Lixivium
#Google Not sure what you mean with ID. An optgroup is not a item you can select. In Zend the key for an array Group A becomes a label and the label looks like a title in the drop down menu. See W3SchoolsPreter
I am interested in the question being answered using XML, as it was requested, which would be useful with Zend_Config_XML and Zend_Form_ElementMarsupium
Please note that setMultiOptions() no longer exists in Zend Framework 3. Use Mr Coder's answer instead.Infarct

© 2022 - 2024 — McMap. All rights reserved.