Search code examples
cssformssymfonyattributesformbuilder

How can I add a style to a form element in Symfony 4?


I am working with the formBuilder in Symfony 4:

$form = $this->createFormBuilder($item)
    ->add('username', TextType::class, array('attr' => array('class' => 'form-control')))

I want to add a style to the form:

$form = $this->createFormBuilder($item)
    ->add('username', TextType::class, array('attr' => array('style' => 'margin-right:5px'), 'attr' => array('class' => 'form-control')))

I do not see the style. It is not added to the field...


Solution

  • You have twice the array key attr so change it to:

        ->add('username', TextType::class, array('attr' => array('class' => 'form-control','style' => 'margin-right:5px')))