Adding a link to a form element with Zend Framework
The problem I was stumped on friday was stupid enough. We have a form builder using Zend_Form that builds a form based on external XML input. In certain situations, a form field needs to have an associated link with it.
I was quickly told by the friendly people in #php_bnl on irc.freenode.net that I needed to use Decorators for that, so I went diving into the documentation. Reading the official documentation, I couldn't really get into how exactly the decorators worked and what I could do with them, however, DevZone to the rescue, because Matthew wrote an excellent article there on using the Decorators with Zend_Form. After reading through this article, I quickly understood how things worked, and had easily solved my problem. Well, almost.
I got as far as setting the decorator order using $element->setDecorators(array()), and then, since I was using the Description decorator for this purpose, using $element->setDescription() to add my link to the form. All seemed well, except for one thing: Instead of a link being added to the form, I got the escaped HTML output.
Diving back into the documentation, I really couldn't figure out how to solve this. I knew there should be an easy way to do it, but I couldn't figure it out. on IRC, I mostly got suggestions telling me to extend the element class I was trying to add a link to, but that seemed overkill for such a small exception. I tweeted my frustration, and quickly got a response from Matthew that I should e-mail him.
It seems I was indeed very close and there is indeed a very simple solution to the problem: you need to set the decorator with an additional configuration parameter. So, this will work:
$element->setDecorators(array(array('Description', array('escape' => false))));
Of course a note needs to be put here: Don't use this easily. You *are* turning off the escaping, so you need to be sure that whatever you put into the description is save data. But, being the slightly pragmatic developer that I am, for a situation like this, I much prefer this approach over having to subclass an element and override quite a few of the original methods of the element.