Zend Framework

Add overloading to decorators to Zend_Form

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.0, 1.5.1
  • Fix Version/s: 1.6.1
  • Component/s: Zend_Form
  • Labels:
    None
  • Fix Version Priority:
    Should Have

Description

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

<table>
    <tr>
        <td><?= $this->form->foo->renderLabel() ?></td>
        <td>
            <?= $this->form->foo->renderElement() ?>
            <?= $this->form->foo->renderDescription() ?>
            <?= $this->form->foo->renderErrors() ?>
        </td>
    </tr>
</table>

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));
}

Activity

Hide
Cristian Bichis added a comment -

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

Show
Cristian Bichis added a comment - I think rendering to table (and generally speaking using tables on XHTML) is obsolete in 2008...
Hide
Eric Coleman added a comment -

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

Show
Eric Coleman added a comment - Cristian... the ticket isn't about "tables" it's about the extensions to form rendering. I'd love to see this myself.
Hide
Torsten Bühl added a comment -

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

Show
Torsten Bühl added a comment - 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
Hide
Goran Juric added a comment -

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

Create a view helper:

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

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

Show
Goran Juric added a comment - Haven't tried it, but I think you should be able to do something like this: Create a view helper: [code] <table> <tr> <td><?= $this->element->getLabel() ?></td> <td> <?= $this->element->render() ?> <?= $this->element->getDescription() ?> <?= $this->element->getErrors() ?> </td> </tr> [/code] and add it to the element (or extend Zend_Form and set this to all elements) via a ViewScript decorator.
Hide
Matthew Weier O'Phinney added a comment -

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.

Show
Matthew Weier O'Phinney added a comment - 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.
Hide
Matthew Weier O'Phinney added a comment -

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

Show
Matthew Weier O'Phinney added a comment - Patch applied to trunk and 1.6 release branch; will release with 1.6.1

People

Vote (14)
Watch (12)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
30m
Original Estimate - 30 minutes
Remaining:
30m
Remaining Estimate - 30 minutes
Logged:
Not Specified
Time Spent - Not Specified