Zend Multiselect Element is posting only one selected values
Asked Answered
B

3

1

I am creating multiple select element like this and it is showed successfully on 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 above element it shows the following html in 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 selected fields and print_r the result like this:

    $request = $this->getRequest();
    $form = new Patient_Form_Patient( $formOptions );

    if ( $request->isPost() ) {
        if ( $form->isValid( $request->getPost() ) ) {
            $values = $form->getValues();
            print_r($values);die();
        }
    } 

It stores only first selected option in array but not all selected elements:

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

    [save] => Submit
)

Can someone help that how can I submit multiple values ?

Buttock answered 29/10, 2010 at 13:54 Comment(6)
Can you check the html rendered by ZF? The select should have a multiple attribute in the <select> tag. See the W3C specs for details: w3.org/TR/html401/interact/forms.html#h-17.6Decoder
They wouldn't be able to select two options if the element wasn't set as "multiple"Pettish
@Pettish Brown:How can I set it as 'multiple'. I am using setMultiOptions() function while creating element but should I any thing else. ThanksButtock
@Awan: I'd say the multiple attribute is already set, else you wouldn't be able to select more than one option in your browser.Pettish
@Pettish Brown: I can select multiple options using SHIFT or CTR keys but it submit only one option.Buttock
There is "tabindex" in the generated code. So, that is NOT the code that is actually generates OR the code that generates it is not as you posted. Which is the root of the error ;)Gittern
V
6

I have reconstructed your problem and I got no such 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's 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 it can help you ;)

Valdavaldas answered 4/11, 2010 at 13:54 Comment(0)
D
0

I think your problem is that you use :

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

instead of:

// addMultiOptions
$element->addMultiOptions( array( '1'=>'clinic1', '2'=>'clinic2' ) );
Demicanton answered 3/11, 2010 at 15:4 Comment(2)
Shouldn't make a difference on a new element with no optionsPettish
It did not helped. Same problem.Buttock
P
0

How are you rendering the element in your view?

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

This is usually handled by a parent form or the PrepareElements decorator (sorry, can't remember exactly and can't test this to find out)

Pettish answered 3/11, 2010 at 22:23 Comment(3)
I am rendering my form elements in external phtml file like this: $this->element->clinics. It is rendering successfully but the problem is that it is not working for multiple selections. ThanksButtock
Rendered html is added in question.Buttock
I think we need to see how you are rendering the form in your view. Is it like alifity's answer or something different? Is there any JavaScript that might be interfering with the posted data? What do you see in the Firebug "Net" tab when you submit the form?Pettish

© 2022 - 2024 — McMap. All rights reserved.