dongzhang5006 2017-03-30 14:13
浏览 32

关于类型的symfony的动态形式

I am trying to do a dynamic Form on two choiceList and i've tried to follow the documentation about SYmfony: http://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms but the are using two entities and i have to work with choiceList Can anyone help me about that here is my UserTYpe:

$

builder
    ->add('at', ChoiceType::class, array(
        'label' => ' ',
        'choices' =>array(
            "42consulting.fr" => "42consulting.fr",
            "42consulting.lu" => "42consulting.lu",
            "42mediatvcom.fr" => "42mediatvcom.fr",
            "42consulting.ma" => "42consulting.ma",
            "42consulting.nl" => "42consulting.nl",
        ),
        'attr' => array(
            'class' => 'form-control',
            'placeholder' => 'service',
        ),
    )
);

$formModifier = function (FormInterface $form, User $at= null) {
    $services = null === $at ? array() :$at->getService();

    $form->add('service', ChoiceType::class, array(
                'label' => 'Service',
                'choices' => array(
                    "42Consulting Paris" => "Saint-Mandé",
                    "42Consulting Lux" => "Luxembourg",
                    "42MediaTelecom" => "Issy-Les-Moulineaux",
                    "42Consulting Maroc" => "Casablanca"
                ),
                'attr' => array(
                    'class' => 'form-control service',
                    'placeholder' => 'service',
                ),
            )
    );

};

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

        $formModifier($event->getForm(), $data->getAt());
    }
);

$builder->get('at')->addEventListener(
    FormEvents::POST_SUBMIT,
    function (FormEvent $event) use ($formModifier) {
        $at = $event->getForm()->getData();

        $formModifier($event->getForm()->getParent(), $at);
    }
);

and the error that i'm getting is : Type error: Argument 2 passed to BackBundle\Form\UserType::BackBundle\Form{closure}() must be an instance of BackBundle\Entity\User, string given, called in /home/work/Sites/backoffice-ad/src/BackBundle/Form/UserType.php on line 263 (on this line: $formModifier($event->getForm()->getParent(), $at);)

  • 写回答

1条回答 默认 最新

  • doujing5726 2017-03-31 07:27
    关注

    On the line

    $formModifier = function (FormInterface $form, User $at= null) {
    

    Your $at, as declare, this parameter must be an instance of User entity. But when you call this function, you passed a string:

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

    Have you tried:

    $formModifier($event->getForm(), $data);
    

    I'm not sure, but I think this is the problem when you call getAt(), this method return string. And $data variable is probably an instance of User entity.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等