ZF-3217: Add overloading to decorators to Zend_Form


A number of users have reported that they'd like syntax like the following in Zend_Form:

<?= $this->form->foo->renderLabel() ?> <?= $this->form->foo->renderElement() ?> <?= $this->form->foo->renderDescription() ?> <?= $this->form->foo->renderErrors() ?>

The idea is that they would like to selectively render specific metadata from the element or form.

This could be done fairly easily by using overloading. __call() could intercept methods beginning in 'render', and if the remainder of the method matches a decorator, would render that decorator.

As a sample implementation:

public function __call($method, $args)
    if ('render' == substr($method, 0, 6)) {
        $decoratorName = ucfirst(substr($method, 6));
        if (false !== ($decorator = $this->getDecorator($decoratorName))) {
            $seed = '';
            if (0 < count($args)) {
                $seed = array_shift($args);
            return $decorator->render($seed);
        require_once 'Zend/Form/Exception.php';
        throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
    require_once 'Zend/Form/Exception.php';
    throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));


I think rendering to table (and generally speaking using tables on XHTML) is obsolete in 2008...

Cristian... the ticket isn't about "tables" it's about the extensions to form rendering. I'd love to see this myself.

Hello Matthew,

I am working at a project right now and want to change to Zend_Form from my own Form Implementation. How can I implement this feature quick in the class? I tried to copy & paste your code but it's not working. There is no Element Decorator.

Best, Torsten

Haven't tried it, but I think you should be able to do something like this:

Create a view helper:




<?= $this->element->getLabel() ?> <?= $this->element->render() ?> <?= $this->element->getDescription() ?> <?= $this->element->getErrors() ?> [/code]

and add it to the element (or extend Zend_Form and set this to all elements) via a ViewScript decorator.

Goran -- not quite what the reporter is looking for. The idea is to be able to render an individual decorator at a time, not just the metadata.

Patch applied to trunk and 1.6 release branch; will release with 1.6.1