Code snippets for symfony 1.x

Navigation

Globally format forms

If you are like me, you see the nice Forms chapter, but when you follow the instructions, your forms don't look nice like the examples.

I gleaned the following techniques from various forum posts. I take NO credit for this, it's heavily snatched from other sources.

First, create your format class, which lays out all forms. I use tables in my design, but you can find examples of div based versions elsewhere, )such as here).

/lib/sfWidgetFormSchemaFormatterCustom.class.php:

<?php
class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatterTable
{
  protected
    $rowFormat       = "<tr class='formrow'>\n  <th class='formheader'>%label%:</th>\n  <td class='forminput'>%error%%field%%help%%hidden_fields%</td>\n</tr>\n",
    $errorRowFormat  = "<tr><td colspan='formerror' colspan=\"2\">\n%errors%</td></tr>\n",
    $helpFormat      = '<br />%help%',
    $decoratorFormat = "<table class='form'>\n  %content%</table>";
}
 

The key here is to insert into each HTML element the global classes to format your code. Above, you can see I added the 'form' class to the table, 'formheader' to the header rows, and 'forminput' for each field.

Next, you need to globally enable this formatter for all forms:

/config/ProjectConfiguration.class.php:

<?php
 
require_once dirname(dirname(__FILE__)).'/lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
 
class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    // for compatibility / remove and enable only the plugins you want
    $this->enableAllPluginsExcept(array('sfDoctrinePlugin', 'sfCompat10Plugin'));
    sfWidgetFormSchema::setDefaultFormFormatterName('Custom');
  }
}
 

Naturally, make sure you manage your plugins appropriately. They key line is the:

sfWidgetFormSchema::setDefaultFormFormatterName('Custom');
 

Now, customize your CSS files:

.form {
  padding-top:15px;
}
.formrow {
 
}
.formheader {
  text-align: right;
  font-weight: bold;
  padding-right: 10px;
  vertical-align: middle;
}
td.forminput input {
  width: 300px;
}
.formerror {
 
}
 

Hope this helps others.

by Mike Crowe on 2009-10-24, tagged form  formatter  sfform  widget