0

我创建了一个带有几个字段的 Zend_From。我想做的是把它们放在段落中,这样它们就会自然流动。举个例子

Danny had [select with 1-10] apples and James had [select with 3-20] pears.

我一直在尝试这样做

$elem= $this->registerForm->getElement('danny');

但是在输出中,该元素的值不再包含在表单中。我也认为这可以用 来完成Zend_Form_SubForm(),但找不到任何例子。

4

2 回答 2

3

你不需要一个子表单,只需要一个带有一些特殊装饰器的常规表单,或者删除一些装饰器。

<?php

class Your_Form_Example extends Zend_Form
{

    public function init() {
        // wrap the select tag in a <span> tag, hide label, errors, and description
        $selectDecorators = array(
            'ViewHelper',
            array('HtmlTag', array('tag' => 'span'))
        );

        $this->addElement('select', 'danny', array(
            'required' => true,
            'multiOptions' => array('opt1', 'opt2'),
            'decorators'   => $selectDecorators // use the reduced decorators given above
        ));
    }
}

然后这里是呈现表单的视图脚本......

<form method="<?php echo $form->getMethod() ?>" action="<?php echo $form->getAction() ?>">
  <p>Danny had <?php echo $form->danny ?> apples and James had <?php echo $form->james ?> pears.</p>
  <p>More stuff here...</p>

  <?php echo $form->submit ?>
</form>

这应该会导致类似

<p>Danny had <span><select name="danny" id="danny"><option>opt1</option><option>opt2</option></select></span> apples and James had .....</p>

为了保持表单输出良好,Errors、Description 和 Label 装饰器被移除并且不会被渲染。因此,当您检查表单上的错误时,如果选择元素有错误,则需要将它们显示在表单顶部或其他地方,因为它们不会与选择元素一起呈现。

希望有帮助。

于 2011-10-18T20:59:06.327 回答
0

您甚至可以使用单个字段。您从控制器发送到视图的form变量是一个对象数组。您可以使用->运算符获取单个字段。例如,您可以使用

danny had <?php echo $this->form->danny; ?>apples and james had <?php echo $this->form->james; ?>.......

请注意$this->form->danny$this->form->james您的 html 元素是否放置在您的zend form

于 2011-10-19T12:10:46.210 回答