19

我希望能够使用数组表示法向我的表单添加一个隐藏的表单字段。我可以用这样的 HTML 来做到这一点:

<input type="hidden" name="contacts[]" value="123" />
<input type="hidden" name="contacts[]" value="456" />

当表单被提交时,该$_POST数组将包含分组为数组的隐藏元素值:

array(
    'contacts' => array(
        0 => '123'
        1 => '456'
    )
)

我可以在我的表单中添加一个隐藏元素,并像这样指定数组表示法:

$form->addElement('hidden', 'contacts', array('isArray' => true));

现在,如果我用数组填充该元素,我希望它应该将值存储为数组,并将元素呈现为上面显示的 HTML:

$form->populate($_POST);

但是,这不起作用。我正在使用的 Zend Framework 版本中可能存在错误。我这样做对吗?我应该怎么做?我怎样才能达到上述结果?如果必须,我愿意创建一个自定义表单元素。让我知道我需要做什么。

4

4 回答 4

19

您必须使用子表单来获得您寻求的结果。文档很方便,但你可以在这里找到

使用我在那里找到的内容,我构建了以下表格

 <?php

class Form_Test extends Zend_Form {

    public function init() {
        $this->setMethod('post');
        $this->setIsArray(true);

        $this->setSubFormDecorators(array(
            'FormElements',
            'Fieldset'
        ));

        $subForm = new Zend_Form(array('disableLoadDefaultDecorators' => true));

        $subForm->setDecorators(array(
            'FormElements',
        ));

        $subForm->addElement('hidden', 'contacts', array(
            'isArray' => true,
            'value' => '237',
            'decorators' => Array(
                'ViewHelper',
            ),
        ));

        $subForm2 = new Zend_Form(array('disableLoadDefaultDecorators' => true));

        $subForm2->setDecorators(array(
            'FormElements',
        ));

        $subForm2->addElement('hidden', 'contacts', array(
            'isArray' => true,
            'value' => '456', 'decorators' => Array(
                'ViewHelper',
            ),
        ));

        $this->addSubForm($subForm, 'subform');
        $this->addSubForm($subForm2, 'subform2');


        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setValue('Submit');

        $this->addElement('submit', 'submit');
    }

}

Wich 输出这个 html:

<form enctype="application/x-www-form-urlencoded" method="post" action=""><dl class="zend_form">
<input type="hidden" name="contacts[]" value="237" id="contacts">

<input type="hidden" name="contacts[]" value="456" id="contacts">

<dt id="submit-label">&nbsp;</dt><dd id="submit-element">

<input type="submit" name="submit" id="submit" value="submit"></dd></dl></form>

提交后的帖子如下所示:

array(2) {
  ["contacts"] => array(2) {
    [0] => string(3) "237"
    [1] => string(3) "456"
  }
  ["submit"] => string(6) "submit"
}

所以这就是你如何创造你所寻求的那种形式。希望这可以帮助!如果您有问题发表评论!

如果你问我,它很hackish。您基本上创建了子表单,但禁用了表单装饰器,因此只有元素得到输出。由于相同的 contacts[] 元素采用不同的形式,因此对象 zend 不会覆盖它们并且它可以工作。但是是的..

编辑:对其进行了一些更改以删除隐藏输入周围的标签和垃圾。

于 2010-09-09T00:53:27.040 回答
16

要使用数组表示法,您需要指定元素“属于”父数组:

$form->addElement('hidden', 'contact123', array('belongsTo' => 'contacts', 'value' => '123'));
$form->addElement('hidden', 'contact456', array('belongsTo' => 'contacts', 'value' => '456'));
于 2011-08-15T04:26:47.580 回答
8

这确实似乎是 Zend 框架中的一个错误 - 元素的 value 属性被正确设置为数组,但是当元素呈现时它被忽略 - 它仅用于$this->view->escape($value)输出元素的 html。我通过为这些元素实现自定义助手解决了这个问题:

class My_View_Helper_HiddenArray extends Zend_View_Helper_FormHidden 
{
    public function hiddenArray($name, $value = null, array $attribs = null)
    {
        if (is_array($value)) {
            $elementXHTML = '';
            // do not give element an id due to the possibility of multiple values
            if (isset($attribs) && is_array($attribs) && array_key_exists('id', $attribs)) {
                unset($attribs['id']);
            }

            foreach ($value as $item) {
                $elementXHTML .= $this->_hidden($name, $item, $attribs);
            }
            return $elementXHTML;

        } else {
            return $this->formHidden($name, $value, $attribs);
        }
    }
}

其中,当使用下一种方式时:

$contacts = $form->createElement('hidden', 'contacts')
->setIsArray(true)
->setDecorators(array(
    array('ViewHelper', array('helper' => 'HiddenArray')),
));
$form->addElement($contacts);

生成所需的输出。

在这里扩展 Zend_View_Helper_FormHidden 的原因只是为了能够在没有设置数组值的情况下调用默认行为(return parent::formHidden($name, $value, $attribs))。

希望这可以帮助某人:)

于 2012-01-31T15:56:43.947 回答
0

对于较新版本的 ZF,您应该使用https://framework.zend.com/manual/2.1/en/modules/zend.form.elements.html#multicheckbox

于 2019-03-06T16:16:48.643 回答