Zend_Form_Decorator-如何将属性添加到Zend_Form_Element_Hidden? [英] Zend_Form_Decorator - How to add attrib to Zend_Form_Element_Hidden?

查看:81
本文介绍了Zend_Form_Decorator-如何将属性添加到Zend_Form_Element_Hidden?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个要求:
1)所有隐藏的输入元素都应受到影响,而无需移除标准装饰器。
2)这应该发生而不必在每个元素的基础上进行指定。

I have 2 requirements: 1) All hidden input elements should be affected without removing standard decorators. 2) This should happen WITHOUT having to specify it on a per-element basis.

我需要的是将CSS类附加到DT &如果元素类型为Zend_Form_Element_Hidden,则使用DD标签。

All I need is for a CSS class to be attached to the DT & DD tags IF the element type is Zend_Form_Element_Hidden.

我尝试创建自定义的HtmlTag,DtDdWrapper和FormElement装饰器,但是还没有弄清楚该怎么做。

I've tried creating custom HtmlTag, DtDdWrapper, and FormElement decorators, but haven't been able to figure out how to do it.

默认情况下,它们的显示方式是:

By default they appear this way:

<dt>&nbsp;</dt>
<dd><input type="hidden" name="whatever" value="bling" /></dd>

我希望它们以这种方式出现:

I would like them to appear this way:

<dt class="hidden">&nbsp;</dt>
<dd class="hidden"><input type="hidden" name="whatever" value="bling" /></dd>

这样,它们仍将保留在应有的位置,但不会中断

That way they'll still be where they should be, but they won't interrupt the flow of the document.

不起作用的示例:

class My_Form_Decorator_DtDdWrapper extends Zend_Form_Decorator_DtDdWrapper
{
    public function render($content)
    {
        if ($this->getElement() instanceof Zend_Form_Element_Hidden)
        {
            return '<dt class="hidden">&nbsp;</dt><dd class="hidden">'.$content.'</dd>';
        }
        else
        {
            return parent::render($content);
        }
    }


推荐答案

基于在您所需的示例输出中,我不清楚您为什么要在隐藏元素上包含< dt> ,特别是因为您没有应用任何标签,它们最终成为不必要的标记。根据标签不必要的假设,您可以使用以下命令达到预期的效果:

Based on your desired sample output I'm unclear why you would want to include the <dt> on hidden elements, especially since you're not applying any label, they end up becoming needless markup. Following the assumption that the label is unnecessary, you can achieve the desired effect with the following:

class TestForm extends Zend_Form
{
    protected $_hiddenElementDecorator = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')),
    );

    public function init()
    {
        $this->addElement('hidden', 'hiddenElement0');

        $element = new Zend_Form_Element_Hidden('hiddenElement1');
        $this->addElement($element);

    }

    public function loadDefaultDecorators()
    {
        foreach ($this->getElements() as $element) {
            if ($element->getType() === "Zend_Form_Element_Hidden") {
                $element->setDecorators($this->_hiddenElementDecorator);
            }
        }

        parent::loadDefaultDecorators();
    }
}

以上两个元素都将产生相同的输出以及它们各自的ID。示例:

Both of the above elements will result in the same output with their respective id's. Example:

<dd class="hidden">
    <input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>

<$ c中的 foreach 循环$ c> loadDefaultDecorators()方法在构建表单时将 $ this-> _hiddenElementDecorator 迭代地应用于每个隐藏的表单元素。如果要在多种表单上应用隐藏的元素装饰器,只需使用 $ _ hiddenElementDecorator 变量和 loadDefaultDecorators()方法。

The foreach loop in the loadDefaultDecorators() method iteratively applies $this->_hiddenElementDecorator to each hidden form element when the form is built. If you want to apply the hidden element decorator on multiple forms, just create a parent class with the $_hiddenElementDecorator variable and loadDefaultDecorators() method in it.

但是,如果您愿意设置标签元素(< dt> ),如示例输出中所述,并应用 hidden类,因此最终得到:

However, if you have your heart set on including the label element (<dt>) as described in your sample output and applying the 'hidden' class so you end up with:

<dt class="hidden">&nbsp;</dt>
<dd class="hidden">
    <input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>

...您将需要扩展 Zend_Form_Decorator_Label 类并重写render()方法。看看 if(null!== $ tag)块中的注释:

... you will need to extend the Zend_Form_Decorator_Label class and override the render() method. Take a look at the comment in the if (null !== $tag) block:

class My_Form_Decorator_Label extends Zend_Form_Decorator_Label
{
    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if (null === $view) {
            return $content;
        }

        $label     = $this->getLabel();
        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $tag       = $this->getTag();
        $id        = $this->getId();
        $class     = $this->getClass();
        $options   = $this->getOptions();

        if (empty($label) && empty($tag)) {
            return $content;
        }

        if (!empty($label)) {
            $options['class'] = $class;
            $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
        } else {
            $label = '&nbsp;';
        }

        if (null !== $tag) {
            require_once 'Zend/Form/Decorator/HtmlTag.php';
            $decorator = new Zend_Form_Decorator_HtmlTag();
            // Add 'class' => 'hidden' to the <dt> tag decorator options.
            $decorator->setOptions(array('tag' => $tag, 'class' => 'hidden'));
            $label = $decorator->render($label);
        }

        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $label;
            case self::PREPEND:
                return $label . $separator . $content;
        }

    }
}

最后,要将新的装饰器应用于所有隐藏的表单元素,您需要将元素前缀路径点添加到装饰器,并将标签装饰器添加到 $ _ hiddenElementDecorator TestForm 类中的数组:

Finally, to apply your new decorator to all of the hidden form elements you will need to add the an element prefix path point to your decorator and re-add the label decorator to the $_hiddenElementDecorator array in the TestForm class:

class TestForm extends Zend_Form
{
    protected $_hiddenElementDecorator = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')),
        // Add back the label element.
        array('Label', array('tag' => 'dt', 'class' => 'hidden')),
    );

    public function init()
    {
        $this->addElement('hidden', 'hiddenElement0');

        $element = new Zend_Form_Element_Hidden('hiddenElement1');
        $this->addElement($element);

    }

    public function loadDefaultDecorators()
    {
        foreach ($this->getElements() as $element) {
            if ($element->getType() === "Zend_Form_Element_Hidden") {
                $element->setDecorators($this->_hiddenElementDecorator);
            }
        }

        // Add a decorator prefix path pointing to our new nifty decorator.
        $this->addElementPrefixPath('My_Form_Decorator', '/path/to/Decorator', Zend_Form_Element::DECORATOR);
        parent::loadDefaultDecorators();
    }
}        

像馅饼一样容易,不是吗?

Easy as pie, no?

这篇关于Zend_Form_Decorator-如何将属性添加到Zend_Form_Element_Hidden?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆