How to handle multi-select boxes in a zend framework form?
Asked Answered
D

6

6

Just wondering how it works and how to handle the information.

Let's say I have a form like this:

$multi = new Zend_Form_Element_Multiselect('users');
$multi->setMultiOptions(array(
    //'option value' => 'option label'
    '21' => 'John Doe',
    '22' => 'Joe Schmoe',
    '23' => 'Foobar Bazbat'
));
$form->addElement($multi);

If a user selects one, or multiple values from a multi-select box...

  • How do I get the value that the user selected?
  • Does it return in array? or what?
  • How can I tell how many items the user selected?
Disincline answered 11/12, 2009 at 20:30 Comment(0)
D
11

Using a multi select element like this one:

$multi = new Zend_Form_Element_Multiselect('users');
$multi->setMultiOptions(array(
    //'option value' => 'option label'
    '21' => 'John Doe',
    '22' => 'Joe Schmoe',
    '23' => 'Foobar Bazbat'
));
$form->addElement($multi);

You can get the values of the element like this:

public function indexAction()
{
    $form = new MyForm();

    $request = $this->getRequest();
    if ($request->isPost()) {

        if ($form->isValid($request->getPost())) {

            $values = $form->getValues();
            $users = $values['users']; //'users' is the element name
            var_dump $users;
        }
    }
    $this->view->form = $form;
}

$users will contain an array of the values that have been selected:

array(
    0 => '21',
    1 => '23'
)
Disincline answered 12/12, 2009 at 1:12 Comment(1)
I am doing almost same thing but getting only one selected option even selecting multiple. Here is my question: #4053012Affirm
T
3
$form->getElement('name')->getValue() 

will return the value of $_POST['name']. You can make

$_POST['name'] 

be an array by defining the name of the element with brackets at the end. So in this case, 'name[]'. In the Zend Framework, use an element that extends

Zend_Form_Element_Multi

Please see: http://www.framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.multiselect

For example:

$multi = $form->createElement('multiselect', 'name[]');
$multi->setMultiOptions($options);
$form->addElement($multi);

if ($form->isValid($_POST)) {
    $userSelectedOptions = $form->getElement('name')->getValue();
}
Trudietrudnak answered 11/12, 2009 at 21:0 Comment(0)
D
1

See the answer from brad. The especial part is the name of the element

$multi = $form->createElement('multiselect', 'name[]');

if you call the element with squares it will be handled as an array by the browser (not a zf behavior). Otherwise you'll get only the first selected element

Detruncate answered 26/4, 2012 at 13:14 Comment(0)
J
0

Also one remark, maybe useful for someone here (I spent some time to get it):

If you are creating your own multi checkbox element, you must extend Zend_Form_Element_MultiCheckbox , because validation does not work correctly, when you are extending just Zend_Form_Element_Multi.

Jamnis answered 28/5, 2013 at 20:37 Comment(0)
P
0

It may be helpful to others: I found on Zend Framework 1.12 that if you don't pass the multi element a name ending in [] it throws an error in Zend Form.

E.g.

$this->addElement('multiselect', 'somename'); // throws error

while:

$this->addElement('multiselect', 'somename[]'); // works

Ptisan answered 21/11, 2013 at 6:22 Comment(0)
U
0

use this to handle multi-select boxes in a zend framework form bro :

    $multi->setAttrib('multiple', 'multiple');

so it will be like this in your own code:

    $multi = new Zend_Form_Element_Multiselect('users');
    $multi->setAttrib('multiple', 'multiple');
    $multi->setMultiOptions(array(
        //'option value' => 'option label'
        '21' => 'John Doe',
        '22' => 'Joe Schmoe',
        '23' => 'Foobar Bazbat'
    ));
    $form->addElement($multi);
Unconventional answered 17/10, 2016 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.