Snippet: Symfony Forms - Definition List Form Formatter

Back in April I posted a Snippet on Symfony Form Formatters. Since then I've done a bit more work with them and thought I'd share my custom form formatter for displaying forms as definition lists. Forms are often marked up as unordered lists, but with their label-input structure I find they often make semantic sense as definition lists.

To create the formatter paste this code into a new file and name it sfWidgetFormSchemaFormatterDefList.class.php. I saved it in /lib but it also makes sense to put it in /lib/form, depending on your personal preference.

< ?php // lib/sfWidgetFormSchemaFormatterDefList.class.php class sfWidgetFormSchemaFormatterDefList extends sfWidgetFormSchemaFormatter {
protected $rowFormat = '

', $helpFormat = '%help%', $errorRowFormat = '
', $errorListFormatInARow = '
', $errorRowFormatInARow = '
  • %error%
  • ', $namedErrorRowFormatInARow = '
  • %name%: %error%
  • ', $decoratorFormat = '
    '; } ?>

    To use this formatter in a form you'll need to add the following three lines of code to first add the formatter and assign it to a name, and then set the formatter for the current form.

    < ?php // lib/form/MyModelForm.class.php public function configure() { $customdecorator = new sfWidgetFormSchemaFormatterDefList($this->getWidgetSchema()); $this->getWidgetSchema()->addFormFormatter('deflist', $customdecorator); $this->getWidgetSchema()->setFormFormatterName('deflist'); } ?>

    Please let me know if you have any problems with this snippet, or if you find it useful!

    A Note on Snippets: When using frameworks such as Symfony it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.