symfonytwigsymfony-formssymfony-2.4

Symfony: How to avoid custom form-types getting wrapped in a div automatically?


UserType Form:

class UserType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'email', ['label' => 'EMail']);
        // various other fields....
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('registration'),
            'data_class' => 'Vendor\Model\Entity\User',
        ));
    }

    public function getName()
    {
        return 'form_user';
    }
}

TutorType Form:

class TutorType extends Translate
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UserType(), ['label' => false]);

        $builder->add('school', 'entity', [
            'class' => 'Model:School',
            'property' => 'name',
            'label' => 'Label'
        ]);

        // Various other fields
        $builder->add('save', 'Submit');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            //'validation_groups' => array('registration'),
            'data_class' => 'Vendor\Model\Entity\Tutor',
            'cascade_validation' => true,
        ));
    }

    public function getName()
    {
        return 'form_tutor';
    }
}

When rendering, the UserType is rendered inside a div, i cant find a way to overcome this.

The Form is rendered as

the result

<form name="form_tutor"
      method="post"
      action=""
      novalidate="novalidate"
      class="form-horizontal form-horizontal"
      id="form_tutor">
    <div id="form_tutor"
         novalidate="novalidate"
         class="form-horizontal">
        <div class="form-group">
            <div class="col-lg-10">
                <div id="form_tutor_user">
                    <div class="form-group">
                        <label class="col-lg-2 control-label aaaa required"
                             for="form_tutor_user_email">EMail</label>

                        <div class="col-lg-10">
                            <input type="email"
                                 id="form_tutor_user_email"
                                 name="form_tutor[user][email]"
                                 required="required"
                                 class="form-control" />
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-2 control-label aaaa required"
                 for="form_tutor_tutorType">Type</label>

            <div class="col-lg-10">
                <select id="form_tutor_tutorType"
                     name="form_tutor[tutorType]"
                     class="form-control">
                    </select>
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-2 col-lg-10">
                <button type="submit"
                     id="form_tutor_save"
                     name="form_tutor[save]"
                     class="btn btn-default">Speichern</button>
            </div>
        </div><input type="hidden"
             id="form_tutor__token"
             name="form_tutor[_token]"
             class="form-control"
             value="s6i6zPxJs7KU5CiEe8i6Ahg_ca8rc2t5CnSk5yAsUhk" />
    </div>
</form>

The form_tutor_user is wrapped in a own form-group div. I tried to overwrite the form_tutor_user_widget but this is one level to deep. (And only a quick fix, it should be globally applied to all form type - Classes)

How can i change the theme so all custom types are not wrapped with the default form_row template?

Or how do i know in twig when a "subform" is rendered? so i can decide to print the <div class="form-group"> when the child-node is not a subform, or skip it, if this is the case.

TIA


Solution

  • By default, in the base form theme:

    {% block form_row %}
    {% spaceless %}
        <div>
            {{ form_label(form) }}
            {{ form_errors(form) }}
            {{ form_widget(form) }}
        </div>
    {% endspaceless %}
    {% endblock form_row %}
    

    And, for custom compound forms:

    {% block form_widget_compound %}
    {% spaceless %}
        <div {{ block('widget_container_attributes') }}>
            {% if form.parent is empty %}
                {{ form_errors(form) }}
            {% endif %}
            {{ block('form_rows') }}
            {{ form_rest(form) }}
        </div>
    {% endspaceless %}
    {% endblock form_widget_compound %}
    

    Unless you changed something here, the DIV you see should come from either one or the other bit of template.

    However, in your specfic example, if form_tutor_user_row is defined, the first bit is never used, and if form_tutor_user_widget is defined, the last bit is never used.

    Back to your question. Your question is : "How can i change the theme so all custom types are not wrapped with the default form_row template?"

    Here is the problem the way I see it: you want that your TOP forms (the form in which all sub-forms are included) all have a common way of rendering, in sections. Each section will be included in a DIV with class="form-group". You may want to throw in some additional rendering operations but I will limit myself to this to keep things simple.

    What you need to do then is to create a specfic form type and make all your TOP forms inherit from this new form type. For instance:

    class TopType extends AbstractType
    {
        public function getName()
        {
            return 'top_form';
        }
    }
    

    ... and an inherited form:

    class MyFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            ...
        }
    
        public function getName()
        {
            return 'my_form';
        }
    
        public function getParent()
        {
            return 'top_form';
        }
    }
    

    As you can see, there is no need to make PHP inheritance for form theming inheritance to work.

    Template-theming-wise (can I even say that?), if no specific form theming is set for my_form, Symfony will understand that the default form theme to use here is the form theme of top_form, that you can define as such:

    {% block top_form_widget %}
    {% spaceless %}
        {% for child in form %}
            <div class="form-group">
                {{ form_widget(child) }}
            </div>
        {% endfor %}
        {{ form_rest(form) }}
    {% endspaceless %}
    {% endblock top_form_widget %}
    

    I should add that this is a problem I already encountered and solved. Tell me how that works for you.

    Edit:

    To sum it up, what you have to do is: