I want all form types to have width property, and to use it like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('product_name', 'text', array('width' => "small"));
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);
if (array_key_exists(self::OPTION_WIDTH, $options)) {
$view->vars["attr"]["class"] .= " class_1 class_2 "
}
}
A more generic solution is to create a new Form Type Extension and register it for all types.
The Symfony documentation describes very well how to create a new Form Type Extension: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html
But it fails to mention how to register that extension for all types.
The "trick" is to specify "field" (or "form" from Symfony 2.3 and next) as the extended type (as a result in getExtendedType() and as the alias in the service configuration)
The service configuration:
In Twig you must check whether the new option was set or not: