30
votes

I'm attempting to set the class on a class on a form label using php templates.

Here's my code:

<?php echo $view['form']->label($form['first_name'], 'First Name', array(
    'attr' => array('class' => 'control-label')
)) ?>

But here's my output:

<label class="required" for="form_first_name">First name</label>

I can find how to do it using twig, but not an example with PHP.

1
On this page symfony.com/doc/current/book/… you can see example of passing the class name into twig template, but you can choose to see the code in twig or in pure PHP. The thing is, the code is almost identical to yours so, I'm puzzled as you are. - Marko Jovanović
That's weird indeed as the code seems good to me ... have you modified the default form theme somewhere? which version of twig are you using? Have you tried to clean your application cache using rm -rf? - Geoffrey Brier

1 Answers

70
votes

I've got it...

<?php 
    echo $view['form']->label($form['first_name'], 'First Name', array(
            'label_attr' => array('class' => 'control-label'),
         )) 
?>

Apparently "attr" is "label_attr" for labels fields.

P.S. Corresponding twig code

{{ form_label(form.first_name, 'First Name', { 'label_attr': {'class': 'control-label'} }) }}