Add class attribute in symfony form builder row div

here is my AbstractType code:

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

code 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 a class attribute to a div line:

<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>
+4
source share
4 answers

for me, skler's answer is not a solution, because it adds the class to the input, and not to the div.

edit:

The best decision

{% 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 %}

lead to:

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

it removes unused divs and token labels, and we can use the .registration_field selector instead of .registration_field> div

for Amstell: I respond to the skler solution, saying that for me this is not a solution, Sorry, this is not clear.

+2
source

Symfony2/Twig ; , . , . :

Symfony , ,

. form_row div. , : views/forms/fields.twig.

{% block form_row %}

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

{% endblock %}

, Symfony , .

. form , :

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

, , .

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

+1

app/config/config.yml :

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

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

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

+1

, , :

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

Form\Type:

$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'),
    ));
0

All Articles