3

I have a FormBuilderwith Symfony3 and I need you to add a class to the discount label. How can you observe the third parameter of the add method is an array and a key is attr which has another array with the attributes of the input element, but not the label.

How can I add class to the label?

 $builder
->add('note', TextareaType::class, array(
    'label' => "Notes",
    'required' => false
))
->add('discount', NumberType::class, array(
    'required' => false,
    'attr' => array(
        'class' => "hidden"
    )
))

2 Answers 2

6

You can see the label_attr parameter in symfony docs

'label'=>"Notes,array('label_attr' => array('class' => 'class_name'))"

Sign up to request clarification or add additional context in comments.

Comments

1

This is what worked for me in Symfony 5.4 : 'label_attr' => ['class' => 'your-label-classes']


Example:

class YourFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
       $builder
            ->add('email', EmailType::class, [
                'label'=>'Your label name',
                'label_attr' => ['class' => 'your-label-classes', 'for'=>"email"],
                'attr' => [
                    'class' => "your-field-classes",
                    'type' => "email",
                ],
            ])
            ...
    }
    ...
}

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.