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 ;-)
August 10, 2008
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.
August 11, 2008
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.
August 11, 2008

Php5_zce_logo

not tested in IE


Upcoming events

I will be speaking 04-09-2009: Symfony Day 2009

Tags

1337 2008 4developers accessibility AdaLovelaceDay09 agavi agile amsterdam apache apple article articles atk atkMetaNode audioscrobbler backwards compatibility bbc beatstad best practices bittorrent book books bughuntday caching cake cal evans cat cerf certificate cfp clear cms community conference conferences continuous integration crisis css custom DbFinderPlugin decorator decorators deployment devdays development directoryindex documentation download dpc dpc09 DPC2008 dreamhost eclipse ed efficiency enterprise event events expertise flickr frameworks freeze frontend fun games germany getting real google googletalk graceful degradation hack hackers howto html http ibuildings icann ide imovie indy internet IPC ipc ipc08 javascript jobeet john peel joomla left on the web lime linux live london loudblog m2ts mac malware mambo marjolein meme meta methodology microsoft movie music mysql namespace namespaces nllgg odmarco open source ORM osx paradiso pear personal pfcongrez photo php phpabstract phpBB phpbb phpbelgium phpbenelux phpgg phpitalia phpnw phpnw08 phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations public qa recruiting refactoring review rewrite ruby on rails schedule script security seven things simplexml slides software sogeti solar standard standards static steer subversion symfony symfonycamp symfonyUnderControlPlugin talk technology techportal tek09 terratec terrorism testfest testing textpattern tips tld tomas usability usergroup vhost video vinyl virus warp weblogging wiki women world world of warcraft writing xml xpath yara year youtube ZCE zemanta zend zend framework zend server zend studio Zend_Form
© 2004 - 2009 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0