简体   繁体   English

Symfony2:初始化EntityType字段为空数据

[英]Symfony2: init EntityType field with empty data

I have a field ( EntityType => select in the view) in my FormBuilder that I want it to be in initialized with empty data so I can fill it after that in the view via ajax. 我在FormBuilder中有一个字段( EntityType =>在视图中选择),我希望它使用空数据进行初始化,以便在此之后通过ajax在视图中填充它。
So I have read symfony's documentation about EntityType and I found the choices attribute that receives an array of data, so I gave it an empty one 'choices' => array() and it did the trick. 因此,我阅读了symfony关于EntityType 的文档 ,并发现了choices属性可以接收数据数组,因此我给了它一个空的'choices' => array()并成功了。
Now the problem is when I submit the form, symfony don't know anymore the type of the field and give me null . 现在的问题是,当我提交表单时,symfony不再知道字段的类型,请给我null
This is the builder: 这是构建器:

$buidler->add('supplier', EntityType::class, array(
                'class' => 'SBC\TiersBundle\Entity\Supplier',
                'attr' => array(
                    'class' => 'uk-select uk-select-supplier'
                ),
                'choices' => array(),
            ))

As you can see the the type of the field is SBC\\TiersBundle\\Entity\\Supplier but after submit symfony gives me null ! 如您所见,该字段的类型为SBC\\TiersBundle\\Entity\\Supplier但是在提交symfony之后,我为null!
What should I do to achieve my goal? 我应该怎么做才能实现自己的目标?

All right, this is the solution: 好的,这是解决方案:
First, I need to pass the EntityManager to my form, and to do this I have created a service: 首先,我需要将EntityManager传递给表单,并为此创建了一个服务:

services:
    payment.supplier.form:
        class: SBC\PaymentBundle\Form\PaymentSupplierType
        tags:
            - { name: form.type, alias: form_em }
        arguments: ["@doctrine.orm.entity_manager"]

Then call the EntityManager in the __construct function: 然后在__construct函数中调用EntityManager

 private $em;
 private $supplier;

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

Second, I need to add two events to the form: 其次,我需要在表单中添加两个事件:
PRE_SUBMIT (to get supplier's code and create Supplier object using the EntityManager): PRE_SUBMIT (获取供应商的代码并使用EntityManager创建Supplier对象):

$builder->addEventListener(
            FormEvents::PRE_SUBMIT,
            function(FormEvent $event){
                $data = $event->getData();
                $code = $data['supplier'];
                $this->supplier = $this->em->getRepository('TiersBundle:Supplier')->find($code);
            }
        );

And finally, use the POST_SUBMIT event to set the supplier object in the submitted data: 最后,使用POST_SUBMIT事件在提交的数据中设置供应商对象:

$builder->addEventListener(
            FormEvents::POST_SUBMIT,
            function(FormEvent $event){
                $object = $event->getData();
                $object->setSupplier($this->supplier);
                $event->setData($object);

            }
        );

Thanx to Виталий Бойко who gave me a hint about the form events. 感谢ВиталийБойко,他给了我有关表单事件的提示。
So this is what I did with my knowledge and if you have a better solution please share it with us. 因此,这就是我所学的知识,如果您有更好的解决方案,请与我们分享。

Symfony by default use security for forms, so if you didn't have choices in form builder, you can't pass custom choices to the form after render only via javascript, because you get not valid form. 默认情况下,Symfony对表单使用安全性,因此,如果您在表单生成器中没有选择,则仅通过javascript渲染后就无法将自定义选项传递给表单,因为您获取的表单无效。 You need to create eventlistener for the form. 您需要为表单创建事件监听器。 Check this link for more information enter link description here , here you can find how to add choices. 检查此链接以获取更多信息,在此处输入链接描述 ,在这里您可以找到如何添加选择。 PS sorry for my English) PS对不起,我的英语)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM