【问题标题】:Dynamic generation for submitted forms: $event->getData() returns null提交表单的动态生成:$event->getData() 返回 null
【发布时间】:2016-10-03 16:23:56
【问题描述】:

我正在尝试将城市添加到从 FOSUserBundle 扩展 BaseUser 的 User 类中,并且我正在遵循此页面中的指南: http://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data

我创建了一个 RegistrationType 类,代码如下:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    /* i setted a name field just to check that this form builder is used then i try to create a user */
    $builder->add('name');
    $builder->add('provincia', EntityType::class, array(
            'class'         => 'AppBundle\Entity\State',
            'placeholder'   => '', #
        ));

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

            $data = $event->getData();

            $state = $data->getState();
            $cities = null === $state ? array() : $state->getCities();

            $form->add('city', EntityType::class, array(
                'class'       => 'AppBundle\Entity\City',
                'placeholder' => '',
                'choices'     => $cities,
            ));
        }
    );
//...
}

问题是,当它在 $data->getState() 中向我抛出错误时,它告诉我“错误:在 null 上调用成员函数 getState()”。 会发生什么?

【问题讨论】:

  • 您能告诉我们您是如何在控制器中声明表单的吗?
  • 嗨 Raphael,我想我不是在声明一个表单,因为我从 fosuserbundle 文档 (symfony.com/doc/current/bundles/FOSUserBundle/…) 中了解到,我所要做的就是创建一个继承自 bundle 的类RegistrationFormType,声明:public function getParent() { return 'FOS\UserBundle\Form\Type\RegistrationFormType';我的整个班级将是:(请看下面的回复,由于此回复中的字符限制,我无法将所有代码放在这里)

标签: forms symfony


【解决方案1】:
class RegistrationType extends AbstractType
{
   public function buildForm(FormBuilderInterface $builder, array $options)
   {
      $builder->add('name');
      $builder->add('state', EntityType::class, array(
            'class'         => 'AppBundle\Entity\State',
            'mapped'        => false,
            'placeholder'   => '', #
        ));

        $formModifier = function (FormInterface $form, State $state= null) {
            $cities = null === $state? array() : $state->getCities();

            $form->add('city', EntityType::class, array(
                'class'       => 'AppBundle\Entity\City',
                'placeholder' => '-Choose a state-',
                'choices'     => $cities,
            ));
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
                $data = $event->getData();

                if($data === null || !method_exists($data, 'getState')) {
                    $formModifier($event->getForm(), null);
                } else {
                    $formModifier($event->getForm(), $data->getProvincia());

                }
            }
        );

        $builder->get('state')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                // It's important here to fetch $event->getForm()->getData(), as
                // $event->getData() will get you the client data (that is, the ID)
                $state = $event->getForm()->getData();

                // since we've added the listener to the child, we'll have to pass on
                // the parent to the callback functions!
                $formModifier($event->getForm()->getParent(), $provincia);
            }
        );
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

真正让我抓狂的是 $event->getData(),根据官方文档示例,它应该带来一些东西,在我的例子中是 null。我已经设法解决这个注册表单的工作,但现在我在配置文件编辑表单 argggghhhhh 上遇到了同样的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2013-06-23
    • 2019-07-07
    相关资源
    最近更新 更多