How do I get the value of a parent Symfony3 form from a child form?

I have a parent form with an embedded form. In the embedded (child) form I am looking to create a drop down field with the options of another entity queried from the database. As part of the query I need to reference the parent entity but not sure how to access that parent object from the child form class.

For example the parent is a $subscriber entity. In my case the parent form actually does not show any properties related to the subscriber just allows you to add or delete the child entity forms. Each child form must have the field as described above but the choices need to be limited to values that the subscriber already has a relationship with.

But this is where my question is. How can I access the $subscriber variable below from the code used in the child form?:

$builder->add('otherEntity', EntityType::class, array(
    'class' => "AppBundle:YetAnotherEntity",
    'label' => "Other Entity",
    'query_builder' => $this->manager->getRepository("AppBundle:OtherEntity")->getOtherEntityBySubscriber($subscriber)

which in turn calls this function in my repository:

public function getOtherEntityBySubscriber($subscriber)
    return $this->getEntityManager()
            'SELECT o FROM AppBundle:OtherEntity o JOIN n.subscriberOtherEntity so WHERE o.subscriber = :subscriber'
        ->setParameter("subscriber", $subscriber)

After jbafford's recommendation: I tried your first option but my problem is that my parent form calls the type CollectionType::class not my custom type... because I plan on making a form that can add multiple children items. I am unable to pass any custom options to the CollectionType. Do I need to extend CollectionType to make my own Type that is capable of taking extra options?

My parent form looks like this: $builder->add('child', CollectionType::class, array( "entry_type" => ChildType::class, "allow_add" => true, "by_reference" => false, "allow_delete" => true)); If I add subscriber as an option above I get an error basically saying its not a valid option. I toyed around with making my ChildType extend CollectionType but I don't think that's what I need to do, and get an error saying: The form's view data is expected to be an instance of class AppBundle\Entity\Child, but is an instance of class Doctrine\ORM\PersistentCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\ORM\PersistentCollection to an instance of AppBundle\Entity\Child.

I think I need another class to extend CollectionType just to put in the above add method but I still want my entry type to be the ChildType::class


One way you can do this, since $subscriber is the subject of your parent form, is to pass the $subscriber as a form option to the child form.

You can define it like this in the child:

class ChildForm extends AbstractType
    public function buildForm(FormBuilderInterface $builder, array $options)
        $subscriber = $options['subscriber'];

    public function configureOptions(OptionsResolver $resolver)

and then pass it from the parent.

If your parent form is the root form, you can get the $subscriber out of $options['data']:

        $builder->add('otherEntity', ChildForm::class, [
            'subscriber' => $options['data'],

If not, you may need to use an event listener to get the form data:

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        $subscriber = $event->getData();
        $form = $event->getForm();

        $form->add('otherEntity', ChildForm::class, [
            'subscriber' => $subscriber,

Need Your Help

How do I flip a column in mysql that has data structured: 'last name, first name'?

sql mysql string

I want to create a new column in a MYSQL table that has Fn Ln instead of Ln, Fn. Most of my data is printed Fn Ln. Change active window between Internet Explorer and Excel excel internet-explorer window foreground

I'm writing a program in VB.NET that opens Excel and Internet Explorer.