开发者

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

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 us开发者_运维问答er 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?


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


$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();
}


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


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.


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜