Twitter Bootstrap for Zend_Form

I’m currently reading Easy PHP Websites with the Zend Framework and noticed that the default HTML produced by Zend_Form is rather odd and difficult to style. For some reason it uses the dd and dt tags, and no matter how much I tried the form ended up all over the place.

You can remove these decorators, as they’re known, but the code ends up being rather unwieldy and repetitive. For each text element you need to add:

$email->removeDecorator('label');
$email->removeDecorator('htmlTag');

Confusingly, buttons have a different decorator:

$submit->removeDecorator('DtDdWrapper');

For a short form, such as the login one used in the book, removing the decorators is a minor irritation, but when it comes to larger forms the process soon becomes a nuisance. Since I use Twitter Bootstrap to provide an initial design framework for most new sites, I wondered if I could configure Zend_Form to produce HTML which would work with the various styles provided by Bootstrap.

Before having a go myself, I figured it was worth quickly searching to see if anyone else had come across the same issue, at which point I found the excellent Bootstrap-Zend-Framework project. All you have to do is add one line to your application.ini file, place the files in your library folder and extend your classes from Twitter_Form instead of Zend_Form. A few minutes later I had beautifully-styled forms and could go back to concentrating on application design rather than fiddling with decorators, HTML and CSS.

2 Comments

  1. Paul

    Thanks, I think I prefer Bootstrap-Zend-Framework but it’s good to know there’s another option available.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.