0

Using Symfony 3 and building a form field like this:

$builder
    ->add('tittle')
    ->add('price');

The result is something like:

<div>
    <label ...></label>
    <input ...>
</div>

and I want the result to include custom css classes in the wrapping div, like:

<div class='wrap-title'>
    <label ...></label>
    <input ...>
</div>
<div class='wrap-price'>
    <label ...></label>
    <input ...>
</div>

Trying to do as described here doesn't offer a way to add a class to the wrapping div, just the things inside it.

I don't want to use the bootstrap themes. I really need to add something of my own as a class there. Is that even possible?

1 Answer 1

4

I hope this answer can helps you:

I do not think it's possible to create div element inside the formbuilder.

If you can use Twig then you can render each of the three parts of the field individually inside a div having your custom class

<div class=wrap-title"">
    {{ form_label(form.title) }}
    {{ form_errors(form.title) }}
    {{ form_widget(form.title) }}
</div>
<div class=wrap-price"">
    {{ form_label(form.price) }}
    {{ form_errors(form.price) }}
    {{ form_widget(form.price) }}
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I was just reading about that here. I'll use this approach. Thanks.
The solution from Mintho433 is nice for one form but if you look for a application-wide solution you should use form themes. Please take a few minutes to read this: symfony.com/doc/current/form/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.