Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2, How to make a form label class/attr different than its input?

Tags:

forms

php

symfony

I would like to build a form with label and inputs, but the class of them should be different. Code below creates the label for the input with same attr:

 public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add('hours', null ,
                  array('attr'=> 
                             array(
                                 'placeholder'=>'Working Hours',
                                 'class'=>'ui-spinner-box') ) )
    }

In my code above the ui-spinner-box will be outputted for both label and input. It will even put placeholder for its label.

So how to make it create attr for label separately so I can output something like below :

<label class="MYCLASSFOR_LABEL"   for="input_id">Hours</label>
<input class="MYCLASSFOR_INPUTS"  type="text" id="input_id" name="" value="" >
like image 494
pmoubed Avatar asked Jun 06 '12 18:06

pmoubed


4 Answers

As mentioned in the documentation:

  • attr : A key-value array that will be rendered as HTML attributes on the field
  • label_attr: A key-value array that will be rendered as HTML attributes on the label

You can set those attributes in twig template or in form builder:

Twig template:

  • for symfony 2.1 and newer use:

    {{ form_label(form.hours, null, {'label_attr': {'class': 'foo'}}) }}
    
  • in the legacy symfony 2.0 it used to be

    {{ form_label(form.hours, { 'label_attr': {'class': 'MYCLASSFOR_LABEL'} }) }}
    {{ form_widget(form.hours, { 'attr': {'class': 'MYCLASSFOR_INPUTS'} }) }}
    

Form builder

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('hours', null, array(
        'label_attr' => array('class' => 'MYCLASSFOR_LABEL'),
        'attr'       => array('class' => 'MYCLASSFOR_INPUTS'),
    ));
}
like image 93
a.aitboudad Avatar answered Nov 11 '22 22:11

a.aitboudad


This works for me in Symfony 2.3:

{{ form_row(form.hours,  {   'label': 'Hours:'
                            ,'label_attr': {'class': 'MYCLASSFOR_LABEL'}
                            ,'attr': {'class': 'MYCLASSFOR_INPUTS'} 
                        }
           )
}}
like image 20
Onshop Avatar answered Nov 11 '22 22:11

Onshop


This may be new, but there's an easy way to do this now:

$builder
    ->add('hours', null , array(
        'attr'=> 
            array(
                'placeholder'=>'Working Hours',
                'class'=>'MYCLASSFOR_INPUTS') 
        ) ,
        'label_attr' => array(
            'class' => 'MYCLASSFOR_LABEL'
        )
    );

The option you're looking for is label_attr.

like image 42
Jeremy Warne Avatar answered Nov 12 '22 00:11

Jeremy Warne


The above is no longer correct, at least in the context I was using it. In Symfony 2.1 the solution is:

{{ form_label(form.item, label|default(null), { 'label_attr': { 'class': 'MYCLASS' } }) }}
like image 6
ornj Avatar answered Nov 11 '22 23:11

ornj