Forms 将链式表单嵌入到另一个表单

Forms 将链式表单嵌入到另一个表单,forms,symfony,event-listener,Forms,Symfony,Event Listener,在我的应用程序中,我有两个bundle UserBundle和LocationBundle,它们之间只有一个关联 LocationFormType链接了country、state、city字段表单可以独立工作,但当我尝试将表单LocationFormType嵌入UserRegistrationForm时,我无法访问country对象来检索相关的国家/地区 Error: Call to a member function getCountry() on a non-object 我喜欢在这两种模

在我的应用程序中,我有两个bundle UserBundle和LocationBundle,它们之间只有一个关联

LocationFormType链接了country、state、city字段表单可以独立工作,但当我尝试将表单LocationFormType嵌入UserRegistrationForm时,我无法访问country对象来检索相关的国家/地区

Error: Call to a member function getCountry() on a non-object
我喜欢在这两种模式下使用LocationFormType,嵌入到另一个表单或独立,有人能帮我修复代码吗

//LocationFormType
class LocationFormType extends AbstractType {
    protected $em;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('country', 'entity', array(
                'empty_value' => '--Choose--',
                'class' => 'AppLocationBundle:Country',
                'query_builder' => function(EntityRepository $er)
                {
                    return $er->createQueryBuilder('c')
                        ->where('c.enabled = 1');
                },
                'label' => 'form.country',
                'translation_domain' => 'AppLocationBundle'
            ));

        $builder->addEventSubscriber(new LocationChainedFieldSubscriber($this->em));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\LocationBundle\Entity\Location'
        ));
    }

    public function getName()
    {
        return 'app_location_form';
    }
}
位置表单事件订阅服务器

//EventSubscriber
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormInterface;
use Doctrine\ORM\EntityManager;
use App\LocationBundle\Entity\Country;
use Doctrine\ORM\EntityRepository;


class LocationChainedFieldSubscriber implements EventSubscriberInterface {

protected $em;

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

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

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

    // Problem occurs here when try to get data, the event data are null
    //How to handle data passed from parent form to child form 

    //Might have an empty account(when we insert a new country)
    $country = $location->getCountry() ? $location->getCountry() : null;
    $this->addElement($form, $country);
}

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

    //The data is not yet hydrated into the entity.
    $country = $this->em->getRepository('AppLocationBundle:Country')->find($data->getCountry());
    $this->addElement($form, $country);
}

protected function addElement(FormInterface $form, Country $country = null)
{

    $states = array();
    if($country)
    {
        //fetch the states form specific a country
        $repo = $this->em->getRepository('AppLocationBundle:State');
        $states = $repo->findByCountry($country, array('name' => 'asc'));
    }

    //Add the state element
    $form->add('state', 'entity', array(
        'choices' => $states,
        'empty_value' => '--Choose--',
        'class' => 'AppLocationBundle:State',
        'mapped' => false
    ));
}

}
将表单位置用作服务

services:
    app_location.form:
    class: App\LocationBundle\Form\Type\LocationFormType
    arguments: ['@doctrine.orm.entity_manager']
    tags:
        - { name: form.type, alias: app_location_form }
嵌入到用户注册表中

//UserRegistrationForm
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
       //other fields here
        ->add('name', 'text')
        ->add('location', 'app_location_form', array(
            'data_class' => 'App\LocationBundle\Entity\Location',
            'label' => 'form.location',
            'translation_domain' => 'AppUserBundle'
        ));

    public function getName()
    {
        return 'app_user_profile';
    }
}
//用户注册表格

$builder->add('location', 'collection', array('location' => new LocationFormType ()));

我没有使用集合的任何OneToMany关系,我不能使用集合,因为位置实体不返回位置数组