Customizing Symfony form rendering by using form formatter

Posted on Category:Uncategorized

Although you can always customize Symfony’s form by using templates, it is not necesarily the most practical solution. You might want to customize the way Symfony renders the form somewhere where the HTML structure is being generated in the first place. That’s where the form formatter comes in.

Form formatter is used by Symfony when generating the HTML structure of the form for rendering in a template. When creating your own form formatters note that they should be located in lib/widget directory, and MUST be named like:


An example of a formatter looks like this:

// lib/widget/sfWidgetFormSchemaFormatterMyformatter.class.php
class sfWidgetFormSchemaFormatterMyformatter extends sfWidgetFormSchemaFormatter
    $rowFormat       = "<tr>\n  <th>%label%</th>\n  <td>%field%%error%%help%%hidden_fields%</td>\n</tr>\n",
    $errorRowFormat  = "<tr><td colspan=\"2\">\n%errors%</td></tr>\n",
    $helpFormat      = '<br />%help%',
    $errorListFormatInARow     = '<ul class="error_list">%errors%</ul>',
    $errorRowFormatInARow      = '<li>%error%</li>',
    $namedErrorRowFormatInARow = '<li>%name%: %error%</li>',
    $decoratorFormat = "<table>\n  %content%</table>";

To apply a formatter to the form insert this to form configuration or setup function:

// lib/form/myForm.class.php
class myForm extends sfForm{
  function  configure() {
    $custom_decorator = new sfWidgetFormSchemaFormatterMyformatter($this->getWidgetSchema());
    $this->widgetSchema->addFormFormatter('Myformatter', $custom_decorator);