Zend Multiselect Element仅发布一个选定值

Awa*_*wan 1 php zend-framework multiple-select zend-form

我正在创建这样的多个选择元素,它在表单上成功显示:

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

渲染上面的元素后,它在html源代码中显示以下html:

<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>
Run Code Online (Sandbox Code Playgroud)

但是当我提交带有两个选定字段的表单并且print_r时,结果如下:

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

    if ( $request->isPost() ) {
        if ( $form->isValid( $request->getPost() ) ) {
            $values = $form->getValues();
            print_r($values);die();
        }
    } 
Run Code Online (Sandbox Code Playgroud)

它仅存储数组中的第一个选定选项,但不存储所有选定元素:

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

    [save] => Submit
)
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我如何提交多个值?

sub*_*ito 6

我重建了你的问题,我没有得到这样的错误.你可以看到我在下面做了什么:

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

}
Run Code Online (Sandbox Code Playgroud)

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;
  }

}
Run Code Online (Sandbox Code Playgroud)

index.phtml

<?php
echo $this->form;
Run Code Online (Sandbox Code Playgroud)

这是调试输出(一个选定项目和两个选定项目)

# 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"
  }
}
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助你;)