How to handle multiposition fields in the form of a zend frame?

Just wondering how it works and how to process information.

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 the user selects one or more values ​​from a field with multiple selections ...

  • How to get the value that the user selected?
  • Does it return to the array? or what?
  • How to find out how many items are selected by the user?
+6
zend-framework zend-form
source share
6 answers

Using a multi-element 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); 

You can get the values ​​of an element as follows:

 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 values ​​that were selected:

 array( 0 => '21', 1 => '23' ) 
+11
source share
 $form->getElement('name')->getValue() 

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

 $_POST['name'] 

- an array 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 

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(); } 
+3
source share

See answer from brad. The special part is the element name

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

if you call an element with squares, it will be processed as an array by the browser (and not as a zf behavior). Otherwise, you will receive only the first selected item.

+1
source share

Also one note is probably useful for someone here (I took some time to get it):

If you create your own checkbox, you must extend Zend_Form_Element_MultiCheckbox , because the check does not work correctly when you expand only Zend_Form_Element_Multi .

0
source share

This may be useful to others: I found in Zend Framework 1.12 that if you don't pass in a multi-element name ending in [], it throws an error in the Zend form.

eg.

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

but

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

0
source share

use this to handle multiple-select fields in the form of a zend frame:

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

so it will look like this:

  $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
source share

All Articles