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

15 01 2009

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 …


Actions

Information

15 responses

15 01 2009
Faheem Abbas

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.

19 01 2009
Ahsan Shahzad

Thanks brother :) ,

20 01 2009
Mike

cool article, thanks

15 02 2009
Sergej 'ZaZa' Kurakin

Big big thank you! Saved me from long and boring diggin inside the Zend Framework code.

16 02 2009
Ahsan Shahzad

yes, it takes time without any guidelines :)

25 02 2009
Calibhaan

Thx guy ^^

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

Now, the drop-down… :p

27 05 2009
zend dev

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

3 09 2009
miguel

thank you, it’s such a very useful info

9 12 2009
E

How do you enable default items being selected?

24 02 2010
Dhaval

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…….!!!!!!!

11 05 2010
India

Dhanyawaad :)

17 08 2010
muzeeb

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

3 11 2010
Naveed

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.

4 05 2011
Ritu Srivastava

Hi i am having problem with populating dropdown in jquery in zend pls help mee

6 05 2011
golbarg

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 )

Connecting to %s




Follow

Get every new post delivered to your Inbox.