Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 动态生成已提交表单:$event->;getData()返回null_Forms_Symfony - Fatal编程技术网

Forms 动态生成已提交表单:$event->;getData()返回null

Forms 动态生成已提交表单:$event->;getData()返回null,forms,symfony,Forms,Symfony,我正在尝试将一个城市添加到一个用户类中,该类从FOSUserBundle扩展BaseUser,我遵循本页中的指南: 我创建了一个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 t

我正在尝试将一个城市添加到一个用户类中,该类从FOSUserBundle扩展BaseUser,我遵循本页中的指南:

我创建了一个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()。 会发生什么

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()这个事实,在我的例子中,根据官方文档示例,它应该会带来一些东西,但它是空的。我已经设法解决了这个注册表单的问题,但是现在我在配置文件编辑表单argggghhh上遇到了同样的问题


真正让我抓狂的是$event->getData()这个事实,在我的例子中,根据官方文档示例,它应该会带来一些东西,但它是空的。我已经设法解决了这个注册表单的问题,但是现在我对配置文件编辑表单argggghhh有同样的问题。你能告诉我们你如何在控制器中声明你的表单吗?嗨,拉斐尔,我想我不是在声明表单,因为我从fosuserbundle文档中了解到()我所要做的就是创建一个从bundle的RegistrationFormType继承的类,声明:公共函数getParent(){return'FOS\UserBundle\Form\Type\RegistrationFormType';}我的整个类将是:(看看下面的响应,我不能把所有代码放在这里,因为这个响应中的字符限制)您能告诉我们如何在控制器中声明表单吗?嗨,Raphael,我想我不是在声明表单,因为我从fosuserbundle文档()中了解到,我所要做的就是创建一个从捆绑包的RegistrationFormType继承的类,声明:public function getParent(){return'FOS\UserBundle\Form\Type\RegistrationFormType';}我的整个类将是:(看看下面的响应,由于此响应中的字符限制,我无法将所有代码放在这里)