Musings of ErisDS
beta
ErisDS

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
  <?php
 // lib/sfWidgetFormSchemaFormatterDefList.class.php
class sfWidgetFormSchemaFormatterDefList extends sfWidgetFormSchemaFormatter {
    protected
      $rowFormat                 = '<dt>%label%<dd>%error%%field%%help%%hidden_fields%</dd>',
      $helpFormat                = '<span class="help">%help%</span>',
      $errorRowFormat            = '<dt class="error">Errors:</dt><dd>%errors%</dd>',
      $errorListFormatInARow     = '<ul class="error_list">%errors%</ul>',
      $errorRowFormatInARow      = '<li>%error%</li>',
      $namedErrorRowFormatInARow = '<li>%name%: %error%</li>',
      $decoratorFormat           = '<dl id="formContainer">%content%</dl>';
}
?>

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.

1
2
3
4
5
6
7
8
9
  <?php
 // lib/form/MyModelForm.class.php
 public function configure()
  {
    $custom_decorator = new sfWidgetFormSchemaFormatterDefList($this->getWidgetSchema());
    $this->getWidgetSchema()->addFormFormatter('deflist', $custom_decorator);
    $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.

Related Posts

Share this...

  •  Add 'Snippet: Symfony Forms - Definition List Form Formatter' to Del.icio.us
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to Twitter
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to digg
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to FURL
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to reddit
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to Technorati
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to Newsvine
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to Stumble Upon
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to Google Bookmarks
  • Add 'Snippet: Symfony Forms - Definition List Form Formatter' to FaceBook

Comments

11 Comments to "Snippet: Symfony Forms – Definition List Form Formatter"
  1. Twitted by ErisDS

    2nd Jun

    Twitted by ErisDS pinged back:

    [...] This post was Twitted by ErisDS – Real-url.org [...]

  2. 10th Jun

    Jenny says:

    I just found your blog through someone else’s site and I just wanted to say hai! :)

  3. 10th Jun

    ErisDS says:

    Hai back at ya :) I can so guess who’s blog you found me on!!

    Am now following you on twitter :)

  4. 17th Jun

    xz says:

    Great snippet!
    seems to work fine for me.

  5. 17th Jun

    ErisDS says:

    @xz I’m glad to have helped. If you do have any problems with the snippet be sure to let me know :)

  6. 22nd Jun

    Oncle Tom says:

    In Symfony 1.2, you even don’t need to declare the form formatter.

    It will use the pattern sfWidgetFormSchemaFormatterformatterName. So if I create a

    1
    sfWidgetFormSchemaFormatterDiv

    formatter, I can call use it directly with

    1
    ::setFormFormatterName('div')

    .

    Less code for the same thing :)

  7. 9th Jul

    Henrik says:

    Hey

    Have made my own formatter and dammm it took some time to figure it out :) Looks nice, but do you have an example of what it will look like ?

  8. 16th Jul

    ErisDS says:

    @Oncle Tom – Sorry I didn’t reply before. Just got around to trying out your addition – will update the post shortly. Thank you so much for sharing that :)

    @Henrik The way I styled the Definition list was to look a little how it does in the magneto backend if you have ever seen it? I will add a screenshot and some css later to explain better how it works.

  9. 16th Jul

    Henrik says:

    @ErisDs nope sorry :) sweet looking forward to it.

  10. 21st Aug

    Alex says:

    Cant make it work. Damn. Plz contact with me.

  11. 23rd Aug

    ErisDS says:

    What are you having problems with? You can always contact me via the contact form

Add your thoughts

  • XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>