从Zend_Form中删除html标签

时间:2011-06-05 19:59:51

标签: zend-framework zend-form

我想要一个简单的HTML表单:

<form method="post"> 
<input type="text" name="question" id="question" value="" size="50%">
<input type="submit" name="ask" id="ask" value="Ask">
</form>

我使用Zend Framework获得的是:

<form enctype="application/x-www-form-urlencoded" method="post" action=""> 
<dt id="question-label">&#160;</dt> 
<dd id="question-element"> 
<input type="text" name="question" id="question" value="" size="50%"></dd> 
<dt id="ask-label">&#160;</dt><dd id="ask-element"> 
<input type="submit" name="ask" id="ask" value="Ask"></dd>
</form>

如何删除不需要的html标签(dd,dt)?

3 个答案:

答案 0 :(得分:3)

我发现了类似的问题,并在那里回答了我使用以下解决方案(对我有用):

$form->setElementDecorators(array('ViewHelper','Errors'));

答案 1 :(得分:0)

你可以使用 - &gt; removeDecorator(“DtDdWrapper”);删除dt和dd包装器。

答案 2 :(得分:0)

完全控制表单的输出,而不会绕过表单逻辑/过滤/验证 The View:

<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">

    <!-- Errors For question field-->
    <?php echo (NULL != ($errors = $this->form->getElement('question')->getMessages()) ? $this->formErrors($errors) : ''); ?>

    <!-- question field -->
    <?php echo $this->form->getElement('question')->renderViewHelper(); ?>

    <!-- Submit Field -->
    <?php echo $this->form->getElement('submit')->renderViewHelper(); ?>
</form>

<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>"> <!-- Errors For question field--> <?php echo (NULL != ($errors = $this->form->getElement('question')->getMessages()) ? $this->formErrors($errors) : ''); ?> <!-- question field --> <?php echo $this->form->getElement('question')->renderViewHelper(); ?> <!-- Submit Field --> <?php echo $this->form->getElement('submit')->renderViewHelper(); ?> </form>

End

The Form

<?php
class Form_Question extends Zend_Form
{
public function init()
{
    $this->setMethod(self::METHOD_POST);

    $element = $this->createElement('text', 'question');
    $element->setLabel('Question');
    $element->setRequired(TRUE);
    $element->removeDecorator('DtDdWrapper');
    $element->setAttrib('class', 'text');
    $this->addElement($element);

    $element = $this->createElement('submit', 'submit');
    $element->setLabel('Submit');
    $element->setRequired(TRUE);
    $element->removeDecorator('DtDdWrapper');
    $element->removeDecorator('label');
    $this->addElement($element);
}
}

<?php class Form_Question extends Zend_Form { public function init() { $this->setMethod(self::METHOD_POST); $element = $this->createElement('text', 'question'); $element->setLabel('Question'); $element->setRequired(TRUE); $element->removeDecorator('DtDdWrapper'); $element->setAttrib('class', 'text'); $this->addElement($element); $element = $this->createElement('submit', 'submit'); $element->setLabel('Submit'); $element->setRequired(TRUE); $element->removeDecorator('DtDdWrapper'); $element->removeDecorator('label'); $this->addElement($element); } }

end form

the controller action