本文介绍了Zend Multiselect Element 只发布一个选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

渲染上述元素后,它在 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>

但是当我提交带有两个选定字段的表单并 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();
        }
    }

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

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

    [save] => Submit
)

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

推荐答案

我已经重构了您的问题,但没有出现此类错误.你可以在下面看到我做了什么:

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

Application_Form_Patient

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

}

索引控制器.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

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

希望能帮到你;)

这篇关于Zend Multiselect Element 只发布一个选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 21:22