Zend Multiselect Element 仅发布一个选定值

发布于 2024-09-29 23:10:26 字数 1259 浏览 5 评论 0原文

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

$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 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-10-06 23:10:26

我已经重建了你的问题,但没有出现这样的错误。您可以在下面看到我做了什么:

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;

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

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

希望它可以帮助您;)

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

谁的新欢旧爱 2024-10-06 23:10:26

我认为你的问题是你使用 :

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

而不是:

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

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' ) );
污味仙女 2024-10-06 23:10:26

您如何在视图中渲染元素?

根据记忆,如果该元素不是 Zend_Form 的一部分,则需要手动设置其 name 属性以包含方括号,例如 $element->; setName('诊所[]');.

这通常由父表单或 PrepareElements 装饰器处理(抱歉,记不清了,也无法通过测试来找出答案)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文