如何删除Zend表单元素上的所有DtDdWrappers和标签

Moa*_*oak 10 php zend-framework zend-form

我知道我可以像这样单独从每个元素中删除多余的东西

$button ->removeDecorator('DtDdWrapper')
        ->removeDecorator('HtmlTag')
     ->removeDecorator('Label');
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以以zend形式为我的所有元素实现相同的目标?
如何删除包裹表单的dl?

dre*_*010 25

Markus,这是我使用的解决方案似乎运行良好,希望它适合你.

首先,为了呈现没有<dl>标记的表单,我们需要在表单对象本身上设置装饰器.从扩展Zend_Form的类中,您将调用Zend_Form->setDecorators()传递表单装饰器的数组.

从参考指南:

The default decorators for Zend_Form are FormElements, HtmlTag (wraps in a definition list), and Form; the equivalent code for creating them is as follows:

  $form->setDecorators(array(
      'FormElements',
      array('HtmlTag', array('tag' => 'dl')),
      'Form'
  ));
Run Code Online (Sandbox Code Playgroud)

要将表单包装在除dl之外的其他内容中,我们使用上面的装饰器,但是将dl更改为您使用的任何标记,我通常使用稍后将看到div的类form.

接下来,需要处理这些元素.Zend_Form元素具有不同类型元素的不同装饰器.以下元素类型组各自具有各自不同的装饰器集:[提交和按钮],[验证码],[文件],[图像]和[收音机*].无线电装饰器与标准元素非常相似,只是它没有指定for标签内的属性.

所有其他表单元素,文本,密码,选择,复选框等使用相同的默认装饰器集.

要从单个表单元素中删除dd/dt标记,我们需要将自己的一组装饰器应用于它.这是一个不使用dd/dt标签的示例:

array(
    'ViewHelper',
    'Errors',
    array('Description', array('tag' => 'p', 'class' => 'description')),
    array('HtmlTag',     array('class' => 'form-div')),
    array('Label',       array('class' => 'form-label'))
);
Run Code Online (Sandbox Code Playgroud)

这将使用类将每个表单元素包装在div标记中form-div.问题是,你必须将这组装饰器应用于你不希望被包装在dd/dt标签中的每个元素,这可能有点问题.

为了解决这个问题,我创建了一个从Zend_Form扩展的类,并为它提供了一些默认行为和装饰器,它们与Zend_Form的默认装饰器不同.

虽然我们不能让Zend_Form自动将正确的装饰器分配给特定的元素类型(您可以将它们分配给特定的元素名称),但我们可以设置默认值,并让自己从一个地方轻松访问装饰器,因此如果需要要改变,他们可以很容易地改变所有形式.

这是基类:

<?php

class Application_Form_Base extends Zend_Form
{
    /** @var array Decorators to use for standard form elements */
    // these will be applied to our text, password, select, checkbox and radio elements by default
    public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description')),
        array('HtmlTag',     array('class' => 'form-div')),
        array('Label',       array('class' => 'form-label', 'requiredSuffix' => '*'))
    );

    /** @var array Decorators for File input elements */
    // these will be used for file elements
    public $fileDecorators = array(
        'File',
        'Errors',
        array('Description', array('tag' => 'p', 'class' => 'description')),
        array('HtmlTag',     array('class' => 'form-div')),
        array('Label',       array('class' => 'form-label', 'requiredSuffix' => '*'))
    );

    /** @var array Decorator to use for standard for elements except do not wrap in HtmlTag */
    // this array gets set up in the constructor 
    // this can be used if you do not want an element wrapped in a div tag at all
    public $elementDecoratorsNoTag = array();

    /** @var array Decorators for button and submit elements */
    // decorators that will be used for submit and button elements
    public $buttonDecorators = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'div', 'class' => 'form-button'))
    );


    public function __construct()
    {
        // first set up the $elementDecoratorsNoTag decorator, this is a copy of our regular element decorators, but do not get wrapped in a div tag
        foreach($this->elementDecorators as $decorator) {
            if (is_array($decorator) && $decorator[0] == 'HtmlTag') {
                continue; // skip copying this value to the decorator
            }
            $this->elementDecoratorsNoTag[] = $decorator;
        }

        // set the decorator for the form itself, this wraps the <form> elements in a div tag instead of a dl tag 
        $this->setDecorators(array(
                             'FormElements',
                             array('HtmlTag', array('tag' => 'div', 'class' => 'form')),
                             'Form'));

        // set the default decorators to our element decorators, any elements added to the form
        // will use these decorators
        $this->setElementDecorators($this->elementDecorators);

        parent::__construct();
        // parent::__construct must be called last because it calls $form->init()
        // and anything after it is not executed
    }
}

/*
   Zend_Form_Element default decorators:
   $this->addDecorator('ViewHelper')
        ->addDecorator('Errors')
        ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
        ->addDecorator('HtmlTag', array('tag' => 'dd',
                                        'id'  => array('callback' => $getId)))
        ->addDecorator('Label', array('tag' => 'dt'));
*/
Run Code Online (Sandbox Code Playgroud)

现在使用该类,从这个基类扩展所有表单,并像往常一样分配元素.如果你使用Zend_Form_Element_XXX而不是addElement()那么你将需要将一个装饰器作为选项传递给元素构造函数,如果你使用Zend_Form-> addElement,那么它将使用$elementDecorators我们在类中指定的默认装饰器.

这是一个示例,说明如何从该类扩展:

<?php

class Application_Form_Test extends Application_Form_Base
{
    public function init()
    {
        // Add a text element, this will automatically use Application_Form_Base->elementDecorators for its decorators
        $this->addElement('text', 'username', array(
            'label'      => 'User Name:',
            'required'   => false,
            'filters'    => array('StringTrim'),
        ));

        // This will not use the correct decorators unless we specify them directly
        $text2 = new Zend_Form_Element_Text(
            'text2',
            array(
                'decorators' => $this->elementDecorators, // must give the right decorator
                'label' => 'Text 2'
            )
        );

        $this->addElement($text2);

        // add another element, this also uses $elementDecorators
        $this->addElement('text', 'email', array(
            'label'      => 'Email:', 
            'required'   => false,
            'filters'    => array('StringTrim', 'StringToLower'), 
        ));

        // add a submit button, we don't want to use $elementDecorators, so pass the button decorators instead
        $this->addElement('submit', 'submit', array(
            'label' => 'Continue', 
            'decorators' => $this->buttonDecorators // specify the button decorators
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

这显示了一种非常有效的方法来摆脱dd/dt和dl元素并用自己的元素替换它们.为每个元素指定装饰器有点不方便,而不是能够将装饰器分配给特定元素,但这看起来效果很好.

要添加一个我认为你想要做的解决方案,如果你想渲染一个没有标签的元素,只需创建一个新的装饰器并从中省略标签装饰器,如下所示:

$elementDecorators = array(
    'ViewHelper',
    'Errors',
    array('Description', array('tag' => 'p', 'class' => 'description')),
    array('HtmlTag',     array('class' => 'form-div')),
    // array('Label',       array('class' => 'form-label', 'requiredSuffix' => '*'))
    // comment out or remove the Label decorator from the element in question
    // you can do the same for any of the decorators if you don't want them rendered
);
Run Code Online (Sandbox Code Playgroud)

随意请求澄清任何事情,希望这会帮助你.