Zend Multiselect Element sends only one selected value

I create several select elements like this and it successfully displays on the form:

$element = new Zend_Form_Element_Multiselect('clinics'); $element->setLabel("Clinics"); $element->setAttrib( 'style','width: 240px' ); $element->setMultiOptions( array( '1'=>'clinic1', '2'=>'clinic2' ) ); 

After rendering over the element, it displays the following html in the HTML source:

 <select name="clinics[]" id="clinics" multiple="multiple" style="width: 240px" size="5" class="required" tabindex="41"> <option value="1" label="clinic1">clinic1</option> <option value="2" label="clinic2">clinic2</option> </select> 

But when I submit the form with two fields selected and print_r, the result is as follows:

  $request = $this->getRequest(); $form = new Patient_Form_Patient( $formOptions ); if ( $request->isPost() ) { if ( $form->isValid( $request->getPost() ) ) { $values = $form->getValues(); print_r($values);die(); } } 

Saves only the first selected parameter in the array, but not all selected elements:

 Array ( [clinics] => Array ( [0] => 1 ) [save] => Submit ) 

Can someone help, how can I send multiple values?

+1
php zend-framework multiple-select zend-form
source share
3 answers

I reconstructed your problem and I did not have such an error. You can see what I did below:

Application_Form_Patient

 class Application_Form_Patient extends Zend_Form { public function init() { $this->setName('patient'); $element = new Zend_Form_Element_Multiselect('clinics'); $element->setLabel("Clinics"); $element->setAttrib( 'style','width: 240px' ); $element->setMultiOptions( array('1'=>'clinic1', '2'=>'clinic2' ) ); $submit = $this->createElement('submit', 'submit'); $submit->setLabel('Submit'); $this->addElements(array( $element, $submit )); } } 

IndexController.php

 class IndexController extends Zend_Controller { function indexAction() { require_once 'Application/Form/Patient.php'; $form = new Application_Form_Patient(); $request = $this->getRequest(); if ( $request->isPost() ) { if ( $form->isValid( $request->getPost() ) ) { $values = $form->getValues(); Zend_Debug::dump($values); die(); } } $this->view->form = $form; } } 

index.phtml

 <?php echo $this->form; 

here is the debug output (one selected item and two selected items)

 # select one item array(1) { ["clinics"] => array(1) { [0] => string(1) "1" } } # select two items array(1) { ["clinics"] => array(2) { [0] => string(1) "1" [1] => string(1) "2" } } 

Hope this helps you;)

+6
source share

I think your problem is that you are using:

 $element->setMultiOptions( array( '1'=>'clinic1', '2'=>'clinic2' ) ); 

instead:

 // addMultiOptions $element->addMultiOptions( array( '1'=>'clinic1', '2'=>'clinic2' ) ); 
0
source share

How do you visualize an element in your view?

From memory, if an element is not part of Zend_Form , you need to set its name attribute manually to include square brackets, for example $element->setName('clinics[]'); .

This is usually handled by the parent form or the PrepareElements decorator (sorry, I don’t remember exactly and I can’t check it to find out)

0
source share

All Articles