This can be done from several places:
- define in setDefaultOptions method of Form Type class
- As a parameter of
$this->createForm function called from controller.
- define in buildView method of Form Type class
- In template while rendering form
You just to know which option has precedence over another, and how they work.
Option 1: Here is the place to set default options. If no where else set the value this default value will be used.
Example Implementation (As Pivot provided):
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'attr' => array(
'class' => 'form-horizontal-from-default'
),
));
}
Option 2: You already know this, you can provide the attr value while calling $this->createForm from controller(which is actually a shortcut for $this->container->get('form.factory')->create function). When you provide this value, the value set from previous option will overridden.
Example Implementation (As You provided):
$this->createForm($formTypeObject, $entity, array(
'action' => 'URL,
'attr'=>array('class'=>'form-horizontal')
));
Option 3: You can set or override the value set by previous two option here. setting the value in buildView method. Declare following method in your Form Type Class:
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$options['attr']['class'] = 'form-horizontal-from-build-view';
//If you like to keep value set from previous methods you can define like
//$options['attr']['class'] = isset($options['attr']['class'])? $options['attr']['class'] :'';
//$options['attr']['class'] .= ' form-horizontal-from-build-view';
$view->vars = array_replace($view->vars, array(
'attr' => $options['attr'],
));
}
Option 4:: Now the final and ultimate way to set attributes. This has the height priority over other options, as it is done in the final stage while rendering the form.
You can define as follow in your template:
{{ form_start(form, {'method': 'POST', 'attr': {'class': 'form-horizontal-ultimate' }}) }}