2

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

1
  • Just to pre-empt: yes, I know it's possible to attach attributes to just the "label" tag in CSS but in this particular case I need different types of labels in the same form. Commented Feb 11, 2010 at 1:19

2 Answers 2

7

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.