Symfony2表单 - 如何自动填充相关实体的字段?

时间:2016-05-21 14:54:39

标签: php symfony-forms symfony

我正在尝试按照http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

上的symfony2动态表单修改教程

我的架构与他们在教程中使用的架构略有不同。我的成员包括以下关系:

国家(一对多)办事处
办公室(一对多)员工

当我编辑现有员工时,我希望将Office所在的国家/地区作为默认选项加载,除了仅在Office下拉列表中显示该国家/地区内的办事处(除非选择了其他国家/地区, jQuery代码(不包括在内)应该相应地改变它。)

然而,结果;是Country字段仍然显示占位符值而不是Employee的Office的正确国家/地区。 (另一方面,Office下拉列表仅显示该国家/地区的办事处,这意味着$ country-> getOffices()调用正在运行,因此我正在使用正确的Country对象,我似乎无法拥有它默认情况下选择)。

我是否遵循最佳做法?是否有一些我遗漏的东西不允许我在相关实体的表单中设置值?

代码:

class EmployeeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('name')
        ->add('country', EntityType::class, array(
          'class'   =>  'AppBundle:Country',
          'mapped'  =>  false,
          'placeholder' =>  '=== Select a Country ===',
        ))
    ;

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

          // This will be the Employee entity
          $data = $event->getData();

          $office = $data->getOffice();
          $country = null === $office ? array() : $office->getCountry();

          $form->get('country')->setData($country); // I think this is what's not working properly.

          $form->add('office', EntityType::class, array(
              'class'       =>  'AppBundle:Office',
              'placeholder' =>  '=== Select an Office ===',
              'choices'     =>  $country->getOffices(),
          ));
        }
    );
}

2 个答案:

答案 0 :(得分:1)

我有机会快速阅读您引用的教程链接,我认为您对错误发生的位置是正确的。

我认为(但我不确定),这可能是修复:

$office = $data->getOffice();
$offices = null === $office ? array() : $office->getCountry()->getOffices();

$form->add('office', EntityType::class, array(
  'class'       =>  'AppBundle:Office',
  'placeholder' =>  '=== Select an Office ===',
  'choices'     =>  $offices,
));

我只显示您需要更改的相关部分。试一试,看看是否有帮助。

答案 1 :(得分:0)

尝试修改事件的数据:

class EmployeeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('country', EntityType::class, array(
                'class'   =>  'AppBundle:Country',
                'mapped'  =>  false,
                'placeholder' =>  '=== Select a Country ===',
            ))
        ;

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

                // This will be the Employee entity
                $data = $event->getData();

                $office = $data->getOffice();

                // since the country field is not set as "multiple"
                // the data should not be an array but a string
                $country = null === $office ? '' : $office->getCountry();

                $data['country'] = $country(->getName()?);
                $event->setData($data); // may work

                $form->add('office', EntityType::class, array(
                    'class'       =>  'AppBundle:Office',
                    'placeholder' =>  '=== Select an Office ===',
                    'choices'     =>  $country->getOffices(),
                ));
            }
        );
    }
}