Symfony form Event listener deson't work as Embed Form

I’m using symfony 2.3. I have a form “AddressType” with an event listener that works as expected when instantiated alone. But when I embeded AddressType into BusinessType, the address form is not displaying. I don’t understand what is happening exacltly and how can I fix it.
BusinessType

    class BusinessType extends AbstractType {

private $em;

public function __construct($em) {
    $this->em = $em;
}

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {

    // cela suppose que le gestionnaire d'entité a été passé en option
    $entityManager = $options['em'];
    $transformer = new EmailToUserTransformer($entityManager);
    $ActivityTransformer = new ActivityToStringTransformer($entityManager);

    $builder
            ->add('name')
            ->add('description')
            ->add('file')
            ->add('file2')
            ->add('email')
            ->add('fb')
            ->add('twitter')
            ->add('googlePlus')
            ->add('linkedin')
            ->add('tel')
            ->add('mobile')
            ->add('fax')
            ->add('web')
            ->add('address', new AddressType($this->em))
            ->add(
                    $builder->create('user', 'text')
                    ->addModelTransformer($transformer)
            )
            ->add('activity', null, array(
                'empty_value' => 'Sélectionner une activité',
            ))
            ->add(
                    $builder->create('secondary', 'entity', array(
                        'empty_value' => 'Sélectionner une activité secondaire',
                        'class' => 'BiginfoAdminBundle:Activity',
                        'query_builder' => function (EntityRepository $er) {
                            return $er->createQueryBuilder('a');
                        },
                    ))
                    ->addModelTransformer($ActivityTransformer)
            )
            ->add('enabled')
            ->add('medias', 'collection', array(
                'type' => new $options['media_form'],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false))
            ->add('opens', 'collection', array(
                'type' => new $options['open_form'],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'Biginfo\AdminBundle\Entity\Business',
    ));
    $resolver->setDefaults(array(
        'data_class' => 'Biginfo\AdminBundle\Entity\Business',
        'open_form' => 'Biginfo\AdminBundle\Form\OpenType',
        'media_form' => 'Biginfo\AdminBundle\Form\MediaType',
        'cascade_validation' => true
    ));
    $resolver->setRequired(array(
        'em',
    ));
    $resolver->setAllowedTypes(array(
        'em' => 'Doctrine\Common\Persistence\ObjectManager',
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'biginfo_adminbundle_business';
}

}    

AddressType

 class AddressType extends AbstractType {
    
    private $em;
    
    public function __construct($em) {
        $this->em = $em;
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $propertyPathToPostalCode = 'postalCode';
    
        $builder
                ->add('street', 'text', array(
                    'label' => 'Adresse'
                ))
        ;
        $builder
                ->addEventSubscriber(new AddGouvernauratFieldSubscriber($propertyPathToPostalCode, $this->em))
                ->addEventSubscriber(new AddDelegationFieldSubscriber($propertyPathToPostalCode, $this->em))
                ->addEventSubscriber(new AddSectorFieldSubscriber($propertyPathToPostalCode, $this->em))
                ->addEventSubscriber(new AddPostalCodeFieldSubscriber($propertyPathToPostalCode, $this->em))
        ;
    }
    
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Biginfo\AdminBundle\Entity\Address',
        ));
    }
    
    public function getName() {
        return 'address';
    }
    
    }      

AddDelegationFieldSubscriber

class AddDelegationFieldSubscriber implements EventSubscriberInterface {

private $propertyPathToCity;
private $em;

public function __construct($propertyPathToCity, $em) {
    $this->propertyPathToCity = $propertyPathToCity;
    $this->em = $em;
}

public static function getSubscribedEvents() {
    return array(
        FormEvents::PRE_SET_DATA => 'preSetData',
        FormEvents::PRE_SUBMIT => 'preSubmit'
    );
}

private function addDelegationForm($form, $gouvernaurat_id, $delegation = null) {
    $formOptions = array(
        'class' => 'BiginfoAdminBundle:Delegation',
        'empty_value' => 'Sélectionner une délégation',
        'label' => 'Délégation',
        'mapped' => false,
        'attr' => array(
            'class' => 'delegation_selector',
        ),
        'query_builder' => function (EntityRepository $repository) use ($gouvernaurat_id) {
    $qb = $repository->createQueryBuilder('delegation')
            ->innerJoin('delegation.gouvernaurat', 'gouvernaurat')
            ->where('gouvernaurat.id = :gouvernaurat')
            ->setParameter('gouvernaurat', $gouvernaurat_id)
    ;

    return $qb;
}
    );

    if ($delegation) {
        $formOptions['data'] = $delegation;
    }

    $form->add('delegation', 'entity', $formOptions);
}

public function preSetData(FormEvent $event) {
    $data = $event->getData();
    $form = $event->getForm();

    if (null === $data) {
        return;
    }

    /**
     * Le composant PropertyAccess fournit des fonctions pour lire 
     * et écrire depuis/dans un objet ou un tableau en une simple chaîne de caractères.
     */
    $accessor = PropertyAccess::getPropertyAccessor();

    $sector1 = $accessor->getValue($data, 'sector');
    $sector = $this->em->getRepository('BiginfoAdminBundle:Sector')
            ->findOneBy(array('name' => $sector1));
    $delegation = ($sector) ? $sector->getDelegation() : null;
    $gouvernaurat_id = ($delegation) ? $delegation->getGouvernaurat()->getId() : null;

    $this->addDelegationForm($form, $gouvernaurat_id, $delegation);
}

public function preSubmit(FormEvent $event) {
    $data = $event->getData();
    $form = $event->getForm();

    $gouvernaurat_id = array_key_exists('gouvernaurat', $data) ? $data['gouvernaurat'] : null;

    $this->addDelegationForm($form, $gouvernaurat_id);
}

}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.