user1199981
user1199981

Reputation: 428

Zend Framework Class in Custom Form Decorator

I am using a custom form decorator found at: http://code.google.com/p/digitalus-cms/source/browse/trunk/library/Digitalus/Form/Decorator/Composite.php?r=767

At the bottom of the file (line 70) is:

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

I would like to make the DIV class dynamic and passed when I create the elements in my controller. Any built-in ZEND functions I use only modifies the LABEL or INPUT. Here's an example of my element creation:

$decorator = new Composite();

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

Any ideas would be very much appreciated. Thanks for taking the time to look!

Upvotes: 2

Views: 614

Answers (1)

b.b3rn4rd
b.b3rn4rd

Reputation: 8830

Now sure why all CSS classes are hardcoded, if you are allowed to change this current decorator just fix the render() method:

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;
        }
    }
    /* ... */
}

And during element creation:

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

If you don't want to edit existing decorator, just extend it and override render() method...

Upvotes: 2

Related Questions