你不需要一个子表单,只需要一个带有一些特殊装饰器的常规表单,或者删除一些装饰器。
<?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 装饰器被移除并且不会被渲染。因此,当您检查表单上的错误时,如果选择元素有错误,则需要将它们显示在表单顶部或其他地方,因为它们不会与选择元素一起呈现。
希望有帮助。