1

Some code first:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->addEventListener(
        FormEvents::POST_SET_DATA,
        function (FormEvent $event) {

            /** @var Company $company */
            $company = $event->getData();
            $form    = $event->getForm();

            $form->add(
                'paymentTerms',
                'integer',
                array(
                    'label'          => 'Payment Terms',
                    'label_attr'     => array(
                        'class' => 'col-sm-4 control-label'
                    ),
                    'attr'           => array(
                        'class' => 'form-control'
                    ),
                    'error_bubbling' => true
                )
            );
        }
    )->setAction('test action')->setAttributes(array('class' => 'form-horizontal'));
}

Can someone explain why I can't seem to be able to add a css class to the form itself?

In the above example ->setAction('test action') works perfectly fine, but ->setAttributes(array('class' => 'form-horizontal')) doesn't have any effect on the form.

I've tried ->setAttribute('class', 'form-horizontal') as well with no luck. No errors, it's as if it's being ignored. Why?

1 Answer 1

1

You can add the attr definition to your defaults like you normally would to any field:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        ...
        'attr'       => array(
            'class' => 'form-horizontal'
        ),
    ));
}

Anyway, it's best practice to limit style definition to your views. You should add the class attribute to your form like this (documentation):

{{ form_start(form, {'attr': {'class': 'form-horizontal' }}) }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.