【问题标题】:Remove html tags from Zend_Form从 Zend_Form 中删除 html 标签
【发布时间】:2011-06-05 19:59:50
【问题描述】:

我想要一个简单的 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 框架得到的是:

<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)?

【问题讨论】:

    标签: zend-framework zend-form


    【解决方案1】:

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

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

    【讨论】:

    • 你还需要使用$form-&gt;setDecorators('FormElements', 'Form');来去除DL标签。
    • whit 一些改变使用这个:$this->setDecorators(array('FormElements', 'Form'));
    【解决方案2】:

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

    【讨论】:

    • 我尝试过使用 $form->removeDecorator(“DtDdWrapper”);但这没有效果。
    • @kklobucki:你应该在 Zend_Form_Element 上使用它。
    【解决方案3】:

    在不绕过表单逻辑/过滤/验证的情况下完全控制表单的输出 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>
    

    结束

    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);
    }
    }
    

    结束形式

    the controller action

        public function questionAction()
    {
        $form = new Form_Question();
    
        if($this->getRequest()->isPost())
        {
            if($form->isValid($this->getRequest()->getPost()))
            {
                // Do stuff
            }
            else
            {
                $this->_helper->FlashMessenger(array('error' => "Errors! Correct the errors in the form below"));
            }
        }
    
        $this->view->assign('form', $form);
    }
    

    结束控制器操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      相关资源
      最近更新 更多