dpsyssiv90846 2016-02-09 14:52
浏览 52
已采纳

Symfony2删除FormType中的空字段

I have a onPreSubmit in my Symfony2 FormType.

public function onPreSubmit(FormEvent $event)
{
    $data = $event->getData();

    // Avoid sending empty RegisterProduct
    foreach ($data['registerProducts'] as $key => $registerProduct) {

        if (empty($registerProduct['quantity'])) {
            unset($data['registerProducts'][ $key ]);
        }
    }

    $event->setData($data);
}

The purpose is to remove an array value if a certain fiels in this one is empty (which works)

But when I set the data I have this error:

An exception occurred while executing 'INSERT INTO register_product ..... 
Integrity constraint violation: 1048 Column 'register_id' cannot be null

I don't understand why.

EDIT: I tried with onPostSubmit, but still have the same error

public function onPostSubmit(FormEvent $event)
{
    /** @var Register $register */
    $register = $event->getForm()->getData();

    foreach ($register->getRegisterProducts() as $registerProduct) {

        if ($registerProduct->getQuantity() < 1) {
            $register->removeRegisterProduct($registerProduct);
        }
    }
}
  • 写回答

1条回答 默认 最新

  • doulianxi0587 2016-02-09 15:23
    关注

    As say in comments, you should do it in postSubmit instead of preSubmit.

    Example :

    public function onPostSubmit(FormEvent $event) {
        $data = $event->getData();
    
        foreach ($data['registerProducts'] as $key => $registerProduct) {
    
            if (empty($registerProduct['quantity'])) {
                unset($data['registerProducts'][$key]);
            }
        }
    });
    

    If the SQL error persists, try to replace $event->getData() by $event->getForm()->getData() , the first returns data from the client.

    EDIT

    Unfortunately, the FormEvent doesn't work.

    Also, I'll propose to use the prePersist and preUpdate hooks in your Entity.
    It may not be an alternative for you if your question is only about the form context.

    Usage :

    /**
     * @ORM\HasLifecycleCallbacks
     * @ORM\Entity
     */
    class YourEntity
    {
        // ...
    
        /**
         *
         * @ORM\PrePersist
         * @ORM\PreUpdate
         */
        public function manageProducts()
        {
            foreach ($this->getRegisterProducts() as $registerProduct) {
                if ($registerProduct->getQuantity() < 1) {
                    $this->removeRegisterProduct($registerProduct);
                }
            }
        }
    }
    

    See the Events part of doctrine documentation.

    I hope you can use this solution and that it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站