How to add a custom class to form field in Symfony2

Today I needed to add a custom class to textarea field, to achieve TinyMCE field rendering (with help of http://symfony2bundles.org/stfalcon/TinymceBundle). It wasn’t such straightforward like I thought… I tried:

But it threw exception, that attribute „class” is undefined, so I need to solve it in other way. Thank god I use Twig, so I tried form theming (in template with form). Only thing you need to do is to find out html ID of the textarea field (in my case: wowo_bundle_newsletterbundle_newslettertype_mailing_body). Then you need to add a „_” on the front and „_widget” on the end and class=”tinymce” is there for you.

The solution is simple and powerful, Symfony2 has proven its quality again 🙂

UPDATE 19.10.2011
As Florian and Stloyd mentioned in comments, setting custom class can be done in elegant way, which ain’t included in the documentation yet.

UPDATE 29.10.2011

After my contribution, „attr” element is now documented in official Symfony2 docs http://symfony.com/doc/current/reference/forms/types/field.html

Read more