6

here is my AbstractType code :

   $builder->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))

code above generated :

    <form class="fos_user_registration_register form-horizontal" role="form" method="POST" action="/app_dev.php/register/">

<div id="fos_user_registration_form">
 <div>
   <label class="required" for="fos_user_registration_form_email">Email: </label>
   <input id="fos_user_registration_form_email" type="email" required="required" name="fos_user_registration_form[email]">
 </div>
</div>

</form>
</div>

my question is how to add class attribute to the row div become :

<div class="form-group">
    <label class="required" for="fos_user_registration_form_email">Email: </label>
    <input id="fos_user_registration_form_email" type="email" required="required" name="fos_user_registration_form[email]">
</div>
Yusuf Ibrahim
  • 1,591
  • 5
  • 21
  • 46
  • Possible duplicate of [Symfony twig how to add class to a form row](https://stackoverflow.com/questions/23011450/symfony-twig-how-to-add-class-to-a-form-row) – Philippe-B- Apr 01 '18 at 13:28

4 Answers4

5

Symfony2/Twig has a wonderful form rendering engine; it'd be a waste to completely override it. As a much more elegant alternative, we can take advantage of the built-in form themes. As per the documentation:

Symfony comes with four built-in form themes that define each and every fragment needed to render every part of a form

We can override the default theme and create our own. This allows us to override the form_row block, and add our own lovely attributes to the surrounding div. Simply put this in a file, say: views/forms/fields.twig.

{% block form_row %}

    <div class="form-group">
        {{ form_label(form) }}
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </div>

{% endblock %}

We don't need to worry about the other declarations, as Symfony will overload only what's changed.

We can now reference it from the page template. Replace form with the name of the form variable passed to the template:

{% form_theme form 'forms/fields.twig' %}

Of course, you're free to add a declaration to grab a form attribute instead of hard-coding the class name.

(Source: http://symfony.com/doc/2.7/cookbook/form/form_customization.html)

Jamie
  • 537
  • 7
  • 12
3

for me the answer of skler is not a solution, because it add the class to the input email, not to the div.

edit:

A better solution

{% for child in form.children|keys %}
    <div class="login_field">
        {% if 'token' not in form_label(attribute(form.children,child)) %}
            {{form_label(attribute(form.children,child)) }}
        {% endif %}
        {{form_widget(attribute(form.children,child)) }}
    </div>
    <br/>
{% endfor %}

result in:

<div class="registration_field">
    <label>e-mail</label>
    <input type="email">
</div>

it remove unused div and the token label, and we can use .registration_field selector instead of .registration_field > div

for Amstell: i answer to the solution of skler, saying that for me it's not a solution, Sorry, it's not clear.

lk77
  • 51
  • 3
  • When you say, "it's not a solution, because..." then it's not an answer. Please update if you can help. – Vedda Dec 28 '15 at 12:29
2

Add into your app/config/config.yml following lines:

twig:
    form:
       resources: ['bootstrap_3_layout.html.twig']

or

twig:
    form_themes:
        - "bootstrap_3_layout.html.twig"

http://symfony.com/blog/new-in-symfony-2-6-bootstrap-form-theme

Pavel
  • 31
  • 3
-1

This question is very similar to this other except for the fact that you should use:

{{ form_start(form), {'attr': {'class': 'form-group'}} }}

Another solution is to define it in Form\Type class:

$builder->add('email', 'email', array(
        'label' => 'form.email',
        'translation_domain' => 'FOSUserBundle',
        'attr' => array('class' => 'form-group'),
        'label_attr' => array('class' => 'if-you-want-also-style-attr-label'),
    ));
Community
  • 1
  • 1
M. Foti
  • 3,156
  • 2
  • 16
  • 14
  • 2
    This will only apply the attribute to the `form` tag. That's not what OP wants. – Jamie Apr 11 '16 at 23:04
  • 1
    Also, you should put the hash inside the form_start declaration, not outside. It won't work otherwise, as that's not valid twig syntax. – Jamie Apr 11 '16 at 23:18
  • 1
    {{ form_start(form), {'attr': {'class': 'form-group'}} }} this will produce an error because of " , " the right {{ form_start(form, {'attr': {'class': 'form-group'}} ) }} – Dev_meno Nov 29 '16 at 17:24