Symfony 1.1 Form Templating

So, I'm tired of how the forms look in symfony 1.1. I mean c'mon, tables? We can do better than that.

Note: we can do better than my solution too, I'm sure, but this is way better than tables!

So inside of /lib/widget, I've created sfWidgetFormSchemaFormatterJosh.class.php which looks a little like:

class sfWidgetFormSchemaFormatterPulse extends sfWidgetFormSchemaFormatter
{

  protected
    $rowFormat      = "<div class=\"form-row\">\n %error%%label%\n %field%%help%\n%hidden_fields%\n</div>",
    $errorRowFormat = "<div class=\"error\">\n%errors%</div>\n";
}


And, from there we need to tell our form to use our own formatter, so in your sfForm (child) class, in either configure() or setup() (I use setup() - I don't know if there is a difference, really. Is there? Anyone?), add the following line:

$this->getWidgetSchema()->setFormFormatterName('pulse');

And that's it! Quite simple really. (When the form is rendered, symfony will look for $class = 'sfWidgetFormSchemaFormatter' . ucfirst($name); (where $name is 'josh', or whatever you called your formatter), and does the rendering using that class!)

For full customisation, have a look in sfWidgetFormSchemaFormatter, and look at the protected variables at the top. They're what you have to play with.

For examples of other implementations, symfony have their own formatters: sfWidgetFormSchemaFormatterTable (default) and sfWidgetFormSchemaFormatterList.

Good luck!

Saturday, February 9. 2008

0 Comments