1

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

Here's my code:

->add('email','text',array(
                    'label_attr' => array(
                        'class' =>  'CUSTOM_LABEL_CLASS'
                    ),
                ))

But here's my output:

<label class="CUSTOM_LABEL_CLASS required">Due Date</label>

how to delete the required prefix ?? thanks ,

* other ptoblem *

<div>
<label class="CUSTOM_LABEL_CLASS required" for="email">Due Date</label><input name="email" id="email" required="required" type="text">
</div>

how to set a class and id to the div tag ?

hz4web
  • 29
  • 1
  • 7
  • Please read this http://stackoverflow.com/questions/6734821/how-to-set-a-class-attribute-to-a-symfony2-form-input – Aram Nov 14 '15 at 23:34

1 Answers1

0

Fields are required by default. You have to specify 'required' => false to avoid it.

->add('email','text',array(
      'required' => false,
      'label_attr' => array(
          'class' =>  'CUSTOM_LABEL_CLASS'
       ),
))

About your second question, the simplest way would be to add code in the template :

{{ form_start(form) }}
    <div class="myClass">
        {{ form_widget(email) }}
    </div>
{{ form_end(form) }}

Or create a new specific form block, or also modify existing one : http://symfony.com/doc/current/cookbook/form/form_customization.html#form-theming

sdespont
  • 13,915
  • 9
  • 56
  • 97
  • thanks , the solution of 'required' => 'false' , work for me :) but showing of the div doesn't : it show me :
    thanks again ,
    – hz4web Nov 15 '15 at 07:51
  • Great, last step : https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – sdespont Nov 15 '15 at 07:51
  • yeah , but can you give me solution for the div class ? – hz4web Nov 15 '15 at 07:54
  • @hz4web You need to use the workaround explained in the answer or create a new twig block template : http://symfony.com/doc/current/cookbook/form/form_customization.html#form-theming – sdespont Nov 15 '15 at 07:56