Change field options of an embedded Symfony2 form

Viewed 26787

Is it possible to change an option of a field of an embedded form from the parent form?

To illustrate the problem consider this: I have a parent form type class like this:

class FruitFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('apple', new AppleFormType())
        ;
    }

and a child form type class that is in a separate bundle and I would prefer not to edit, like this:

class AppleFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('qty', 'integer', array('label' => 'rubbish label')
        ;
    }

and I want to change the label of qty to something else, but I want to do this only in the FruitForm, not everywhere where the AppleForm is used. I had hoped to be able to do something like:

class FruitFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('apple', new AppleFormType(), array('qty' => array('label' => 'better label')))
        ;
    }

or:

class FruitFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('apple', new AppleFormType())
        ;

        $builder->get('apple')->get('qty')->setOption('label', 'better label');
    }

but neither of these (and a number of other attempts) have all failed me. There does not exist a setOption method that I can see.

Does anyone know of a way of doing this?

5 Answers
Related