Slowwie
Slowwie

Reputation: 1246

Why do I need the Symfony formbuilder? Whats the benefit?

My question is, for what do I need this formbuilder? Is it not enough for programmers like me, who has written the last years plain PHP code with simple CSS and HTML, that I have to learn Twig and to went into the Symfony system with all the learning stuff. Now I can't use Twig for forms. Ok, I learned this formbuilder stuff, too. No, problem. I have an idea of this now an I build my forms like this, but I can not see the benefit of it? If you write it in Twig, you can reuse it too, hmm ?

Perhaps there is a thing I just overlooked.

Upvotes: 1

Views: 877

Answers (1)

pmihova
pmihova

Reputation: 68

The question is old, but still relevant and does not have a good answer yet, so I will try to give you some advantages and reasons to use formBuilder.

Form validation The data in your form is validated against the constraints of the entity properties. Quite nice and easy.

Entity choice type You can preload select boxes with entities without any effort.

DataTransformers You can apply certain checks, data conversions and formatting between view/norm/model data.

Example: You get ISO8601 string delivered from database -> [magic happens here] -> php DateTime Object to work with -> [more magic] -> nicely Intl formatted and localized date for your form field.

+ optionsResolver

... and if you are willing to go a little further with Twig, you can basically customize the rendering of any form element, in any way you can think of.

Example: with custom defined FormType and twig form widget, a simple

->add('devices', 'device_selection', array(
                'site' => $deviceGroup->getSite(),
                'groups' => false,
                'group' => $deviceGroup,
            ))

in your form builder, can deliver you a list of checkboxes, each wrapped in a div, with icons and additional details. Just an example.

This is just on the top of my head.

I used to do the PHP/HTML/CSS-thing, but when you have a huge, complicated web project on your hands, a little added complexity by the formBuilder adds enormous flexibility. This is where frameworks like Symfony shine.

Upvotes: 3

Related Questions