cretthie
cretthie

Reputation: 349

Symfony twig div

I made a form with the FormBuilder of Symfony. When I put my form in twig, the form_start(form) and form_end(form), it add a tag for each input.

I don't understand why twig adds a tag. What is the solution to remove this tag

Thanks for your answer :) Also, my formbuilder is like that :

->add('title', TextType::class, array(
      'label'=>false,
      'attr'=>array('autofocus'=>true)
      ))

my twig is like that :

{{ form_start(form) }}
<div class="row">
    <div class="col-sm-9 p-1">
        {{ form_row(form_record.title, {'attr':{'class':"form-control", 'placeholder':"Description"|trans, 'title':"Description"|trans }}) }}
        {{ form_errors(form_record.title) }}
    </div>
    <div class="col-sm-1 pt-2">
        <button type="submit" class="btn btn-success btn-circle btn-sm">
            <i class="fas fa-plus"></i>
        </button>
    </div>
</div>
{{ form_end(form) }}

and the result in the html source code is :

<div class="row">
    <div class="col-sm-9 p-1">
        <div>
            <input type="text" id="app__title" name="app_[title]" required="required" class="form-control" placeholder="Description" title="Description">
        </div>
    </div>
</div>

So Twig add the

<div> 

that I don't want. How can I remove this autocompleted tag? I tried the

{% do form_record.title.set rendered %}

but maybe I think that it does not work.

Upvotes: 1

Views: 622

Answers (1)

Tortus
Tortus

Reputation: 151

Edit: Okay it seems I misunderstood the issue at first. I thought you wanted to hide a field you had in your form which can be done with

{% do form.myField.setRendered %}

Now that I understand the issue, I believe it comes from the way your field is being printed.

There are 3 main components to a form field.

Label: form_label(form.field)
Widget: form_widget(form.field)
Errors: form_errors(form.field)

There is a way to print all three components at once. The function is

form_row(form.field)

Here comes the culprit: form_row(). Because it normally prints 3 different components, it adds a div around it!

Futhermore, by looking at the form type and seing 'label' => false, I can say that the label was printing at first using form_row.

To avoid having to define 'label'=>false everytime, you can simply print your form field in this manner:

{{ form_widget(form_record.title, {'attr':{'class':"form-control", 'placeholder':"Description"|trans, 'title':"Description"|trans }}) }}
{{ form_errors(form_record.title) }}

You can simply omit {{form_label(form_record.title)}} and it won't print.

On the other hand, I also noticed something that might be okay but seem wrong with the given example.

In the twig you shared, the form starts with {{ form_start(form) }} but then the field is {{ form_row(form_record.title)}}.

From where I come from form_record is undefined here. I would use {{ form_row(form.title)}}

Anyways, the explanation for the difference between form_row and form_widget can be found here: Symfony form differences between row and widget

Enjoy!

Upvotes: 1

Related Questions