自定义表单装饰器中的Zend Framework类

时间:2012-03-26 22:20:02

标签: php forms zend-framework frameworks decorator

我使用的是http://code.google.com/p/digitalus-cms/source/browse/trunk/library/Digitalus/Form/Decorator/Composite.php?r=767

中的自定义表单装饰器

文件底部(第70行)是:

 $output = '<div class="form_element">'
                . $label
                . $input
                . $errors
                . $desc
                . '</div>';

我想在我的控制器中创建元素时使DIV类动态化并传递。我使用的任何内置ZEND函数仅修改LABEL或INPUT。这是我的元素创建的一个例子:

$decorator = new Composite();

        $this->addElement('text', 'start', array(
            'label'      => 'Start Number',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'alnum',
            ),
            'decorators' => array($decorator)
        ));

非常感谢任何想法。谢谢你花时间看看!

1 个答案:

答案 0 :(得分:2)

现在确定为什么所有CSS类都是硬编码的,如果允许你更改当前的装饰器,只需修复render()方法:

class Digitalus_Form_Decorator_Composite
{
    /* ... */
    public function render($content)
    {
        $element = $this->getElement();
        if (!$element instanceof Zend_Form_Element) {
            return $content;
        }
        if (null === $element->getView()) {
            return $content;
        }

        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $label     = $this->buildLabel();
        $input     = $this->buildInput();
        $errors    = $this->buildErrors();
        $desc      = $this->buildDescription();

        $output = '<div class="'.$this->getOption('class').'">'
                . $label
                . $input
                . $errors
                . $desc
                . '</div>';

        switch ($placement) {
            case (self::PREPEND):
                return $output . $separator . $content;
            case (self::APPEND):
            default:
                return $content . $separator . $output;
        }
    }
    /* ... */
}

在元素创建过程中:

$element->setDecorators(array(
    /* ... */
    array(array('div'=>'Composite'), array('class' => 'my_class_name'))
    /* ... */
)));

如果您不想编辑现有的装饰器,只需将其扩展并覆盖render()方法......

相关问题