dongyihao9887 2017-02-14 09:10
浏览 59
已采纳

Symfony2:init EntityType字段,包含空数据

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.
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.
Now the problem is when I submit the form, symfony don't know anymore the type of the field and give me 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 !
What should I do to achieve my goal?

  • 写回答

2条回答 默认 最新

  • doupeng3501 2017-02-14 13:02
    关注

    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:

    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:

     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):

    $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:

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化