5

I want to add a stylesheet class attribute to most of the fields, but not all.

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('name_short', null, ['attr' => ['class' => 'rtl']] )
        ->add('name_long')
        ->add('profile_education')
        ->add('profile_work')
        ->add('profile_political')
        ->add('twitter')
        ->add('facebook')
        ->add('website')
    ;
}

Is there a simpler way than adding the attribute ['attr' => ['class' => 'rtl']] to every field? Looking for something like looping the fields and setting the attribute after adding the field to the builder.

Thanks for any pointers.

2 Answers 2

2

Came across this and remembered that I recently found a way that works.
Basically iterating over all fields removing and re-adding them with merged options.
Take this example below.

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('name_short')
        ->add('name_long')
        ->add('profile_education')
        ->add('profile_work')
        ->add('profile_political')
        ->add('twitter')
        ->add('facebook')
        ->add('website')
    ;
    
    $commonOptions = array('attr' => array('class' => 'rtl'));
    
    foreach($builder->all() as $key => $field)
    {
        $options = $field->getOptions();
        $options = array_replace_recursive($options, $commonOptions);
        
        $builder->remove($key);
        $builder->add($key, get_class($field->getType()->getInnerType()), $options);
    }    
}   

Edit: Updated to work with Symfony 3, 4, 5 & 6. Thanks @Massimiliano Arione

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

2 Comments

I've try your solution but getting "Could not load type [fieldName]". It works though if I replace the last line with: $builder->add($key, $field->getDataClass(), $options);
Tried with Symfony 3.4, I had to use $builder->add($key, \get_class($field->getType()->getInnerType()), $options); instead
0

You can do this while constructing the form. Simply hold the field names in an array. If you need to assign different field types, then use an associative array instead.

public function buildForm(FormBuilder $builder, array $options)
{
    $fields = array('name_short', 'profile_education', 'profile_work', 'profile_political', 'twitter', 'facebook', 'website');

    foreach ($fields as $field) {
        $builder->add($fields, null, array('attr' => array('class' => 'rtl')));
    }
}

2 Comments

Thanks. Unfortunately that not the answer I was looking for. Is there no way to alter a field once its been added to the builder via add()?
Unfortunately there is no other way. You can dig yourself trough the form builder code, I didn't found a solution.

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.