When I set CSS class in form controller and pass it as $options array element it works.
private function createEditForm(OrderCard $entity)
{
$form = $this->createForm(new OrderCardType($this->get('security.context')
->isGranted('ROLE_SUPER_ADMIN')), $entity, array(
'action' => $this->generateUrl('ordercard_update', array('id' => $entity->getId())),
'attr'=>array(
'class'=>'form-horizontal'
)
));
return $form;
}
But when I want to have same effect using formType it is not added to form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// not works
->setAttribute('attr', array('class' => 'form-horizontal'))
// not works either
->setAttribute('class', 'form-horizontal')
//...
What I do wrong? How to make it work?
This can be done from several places:
$this->createForm
function called from controller.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):
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):
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:
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:
After Symfony 3, you can use:
Add the following to class OrderCardType: