Zend Form :: ‘Multiple Select box’ and ‘Option Group’

Hello Friends,

Multiple Select box in Zend Form using Zend Framework:
I got a scenario in which i was to put drop down with ‘multiple’ option enabled. I am using Zend_Form so  definatley i was in search of some Zend framework provided controll to create it,
i found following  way to create ‘Multiple Select box’ using Zend_Form in Zend framework:

$techDoc = new Zend_Form_Element_Multiselect(‘docs’);
$techDoc->setLabel(‘Technical Documents’)
->setMultiOptions(array(“1″=>”option1″ ,”2″=>”option2”));
$techDoc->size = 5;
I used  Zend_Form_Element_Multiselect to creat ‘Multiple Select box’ so it is very simple 😉 .

First parameter ( $techDoc->setLabel ) will set the Lable of this drop down and second parameter setMultiOptions expects [Associative] array of options that this drop down will have.

Last thing was $techDoc->size that helps to choose size of this Multiple select box.

In following  section, i’ll enhance this example and will try to explain how i achieved ‘Option Group’ in multiselect drop down. (you may also use the following scenario to create ‘Option Group’ in Zend_Form_Element_select that creates simple drop down)

Multiple Select box with ‘Option Group’:

$techDoc = new Zend_Form_Element_Multiselect(‘technical_doc’);
$techDoc->setLabel(‘Technical Documents’)
->setMultiOptions(array(“Option Group 1” => array(“option1″,”option2″,”option3”),
“Option Group 2″=>array(“option1″,”option2″,”option3”)));
$techDoc->size = 5;

so Array of associative arrays generate ‘Option Group’ that was looking very hard to me 🙂 ,

Hope you’ll find it inofrmative …

Advertisements

16 thoughts on “Zend Form :: ‘Multiple Select box’ and ‘Option Group’

  1. Faheem Abbas says:

    Great job.
    Nice to find your blog. Hopefully you will keep it up and we will see some useful stuff here in future.
    Have a nice time.

  2. Calibhaan says:

    Thx guy ^^

    this little word ‘size’ was the solution of my problem… transform a comboBox to a List-Box.

    Now, the drop-down… :p

  3. zend dev says:

    Zend_Form_Element_Select can also be used for mulitple selection.
    all that’s needed to be done is set attribute “multiple” to “multiple” e.g

    $allAgent=new Zend_Form_Element_Select(‘agents’);
    $allAgent->setAttrib(‘multiple’, ‘multiple’);
    Zend_Form_Element_MultiSelect extends Zend_Form_Element_Select( to set this attribute

  4. Dhaval says:

    Nice DuDe !!!

    Is There Any way we can make only readable part in text field and after that we can add other things in zend framework if is there then pls tell me i want to implement in my application i tried lot off but i can’t find solution So Pls Help ME

    Thanks In Advance and also for putting Helpful stuff iin Your Blog…….!!!!!!!

  5. muzeeb says:

    Hi Evryone

    As I had tried to get the chain selection using oracle with respect to parent to child relationship.
    I’m not able to get this using Zend framework, in PHP i’m able to do.
    So if anybody have any idea about this, please help me out.

    Exactly I need is once you select any object in a drop down box automatically it will show the another drop down box with the respective data from oracle.

    Please help me out, if anybody have knows smthng abt it.

    Thanks in advance.

    Best Regards
    Muzeeb

  6. Naveed says:

    It is showing multiple select element successfully on my zend form but when I submit the form after selecting multiple options then it is posting only one value.

  7. golbarg says:

    Usefup tips, thanks.

    To have a default selected value, use ->setValue($optionKey) or ‘value’ => $optionKey if passing form element parameters as an array.

    i.e.:
    $techDoc->setLabel(‘Technical Documents’)
    ->setMultiOptions(
    array(“Option Group 1″ => array(‘key1’ => “option1″, ‘key2’ => ”option2″, ‘key3’ => ”option3″),
    “Option Group 2″=>array(‘key4’ => “option1″, ‘key5’ => ”option2″, ‘key6’ => ”option3″)))
    ->setValue(‘key5’); //adds selected=”selected” to option 2 of group 2

    Values are assigned by default if there is no key provided (0, 1, etc.).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s