Left on the Web

Adding a link to a form element with Zend Framework

At work, I am at the moment working on a Zend Framework project. Opposite to earlier projects with Zend Framework, I am slowly learning of the power behind the framework. But not all of the power is document well enough. On friday, I encountered one such small detail.

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.


Add comment

Comments

gravatar felixdv: Glad you found out a way to make it work! You see, Zend framework has a solution for (nearly) everything ;-)
gravatar Ivo: I'm always slightly amazed at questions like this, and especially the 'frustration' that people seem to get from it.

Had you fired up a step-debugger, wouldn't you have found about the escaping bit in, like, 10 minutes? Surely the code that does the escaping can't be far from your call, and possibly the fact that it checked for this 'escape' paramater would've been clear from the code.

I guess I'm always sort of amazed by the fact that when it comes to frameworks or libraries, people just stop at the api border and don't dare to look into the code.
gravatar Gailin: Most people don't want to go behind the curtain of the API, because they use an API, in theory, to save time. The need to go behind the curtain generally arises when the API is not properly documented, as in the example above.

If you had to dig under the surface of most APIs or libraries you used, then I would hazard a guess that those API would not be used nearly as much.
© 2004 - 2009 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Ma.gnolia, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.2.3