ZF-3217: Add overloading to decorators to Zend_Form
Description
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));
}
Comments
Posted by Cristian Bichis (avantis) on 2008-05-07T05:36:08.000+0000
I think rendering to table (and generally speaking using tables on XHTML) is obsolete in 2008...
Posted by Eric Coleman (eric) on 2008-05-07T05:58:50.000+0000
Cristian... the ticket isn't about "tables" it's about the extensions to form rendering. I'd love to see this myself.
Posted by Torsten Bühl (tbuehl) on 2008-05-21T05:21:37.000+0000
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
Posted by Goran Juric (gog) on 2008-09-08T12:29:20.000+0000
Haven't tried it, but I think you should be able to do something like this:
Create a view helper:
[code]
<
table>
<?= $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.
Posted by Matthew Weier O'Phinney (matthew) on 2008-09-08T12:56:51.000+0000
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.
Posted by Matthew Weier O'Phinney (matthew) on 2008-09-09T12:18:44.000+0000
Patch applied to trunk and 1.6 release branch; will release with 1.6.1