仅渲染<form>标记</form>

时间:2010-07-14 21:26:34

标签: zend-framework zend-form

有没有办法只渲染Zend_Form对象的起始<form>标记?

print $this->registerForm->renderForm();

呈现<form></form>,我只需要<form>

编辑:

在Asleys可能的解决方案之后,我为My_Form类编写了这个

public function renderFormOpen() {
    return str_replace('</form>', '', $this->renderForm());
}

public function renderFormClose() {
    return '</form>';
}

仍在寻找ZF的做法,即使我认为没有 - 在浏览ZF库中的代码之后。

3 个答案:

答案 0 :(得分:5)

可以编写一个自定义的表单装饰器,它使用只呈现打开表单标记的自定义视图助手。但我认为这会有点矫枉过正 只需“硬编码”表单标记,并使用视图中表单变量提供的数据填充属性。

<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
      enctype="<?php echo $this->form->getEnctype() ?>"
      method="<?php echo $this->form->getMethod() ?>"
      id="<?php echo $this->form->getId() ?>"
      class="<?php echo $this->form->getAttrib('class') ?>" >

    <!--in case your products are represented as elements -->
    <?php foreach ($this->form->getElements() as $element): ?>
       <?php echo $element ?>
    <?php endforeach; ?>

    <!--in case your products are represented as displayGroups -->
    <?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
       <?php echo $displayGroup ?>
    <?php endforeach; ?>

    <!--in case your products are represented as subforms -->
    <?php foreach ($this->form->getSubforms() as $subform): ?>
       <?php echo $subform ?>
    <?php endforeach; ?>

    <!--in case your products are rendered by a view helper -->
    <?php foreach ($this->products as $product): ?>
       <?php echo $this->renderProduct($product) ?>
    <?php endforeach; ?>
</form>

只是为了好玩的过度杀戮方式

// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');

// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
//   replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
//   replace method "form" with onlyOpenTagForm"
//   replace
if (false !== $content) {
    $xhtml .= $content
           .  '</form>';
}
//   with:        
if (false !== $content) {
    $xhtml .= $content;
}

完成! - Java-Guys会喜欢它;)

答案 1 :(得分:4)

您可以通过将false传递给表单装饰器来渲染开放表单标记,如下所示:

<?php echo $this->form->renderForm(false) ?>

将输出如下内容:

<form id="post" enctype="multipart/form-data" method="post" action="/post">

另外,你可以将一个字符串传递给表单装饰器,由表单标签包围,如下所示:

<?php echo $this->form->renderForm('Some Text') ?>

输出的内容如下:

<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>

希望这会有所帮助......

答案 2 :(得分:0)

你可以这样做:

echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);