views:

543

answers:

2

Hi,

Is there a simple way to declare CSS classes for Symfony form labels?

This doesn't work:

<?php echo $form['email']->renderLabel('class' => 'my-css') ?>

Found this and it works but it feels counter-intuitive, as it makes the form labels themselves obsolete by requiring the label to be written out in the template:

<?php echo $form['email']->renderLabel('This is a label text', array('class' => 'my-css') ?>

Anyone know a better way?

Thanks

+2  A: 

Passing in null to the first parameter won't override the label text:

<?php echo $form['email']->renderLabel(null, array('class' => 'my-css')) ?>

http://www.symfony-project.org/api/1_4/sfFormField#method_renderlabel

$label The label name (not null to override the current value)

Cryo
Excellent, thanks
Tom
+1  A: 

You can change the formatter for every form by creating a custom class that extends sfWidgetFormSchemaFormatter

    class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
{
  protected
    $rowFormat       = "<tr>\n  <th class=\"my-label-class\">%label%</th>\n  <td>%error%%field%%help%%hidden_fields%</td>\n</tr>\n",
    $errorRowFormat  = "<tr><td class=\"my-error-class\" colspan=\"2\">\n%errors%</td></tr>\n",
    $helpFormat      = '<br />%help%',
    $decoratorFormat = "<table>\n  %content%</table>";
}

You can then change the formatter in the form class:

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

or you can set the new formatter for every form in the config/ProjectConfiguration.class.php file:

sfWidgetFormSchema::setDefaultFormFormatterName('custom');
Radu Dragomir