douchuanghan1344 2015-07-10 22:42
浏览 15

在Symfony 2中重置提交的表单

I'm mapping some data coming from the remote source into my entites. I'm using forms for this purpose. Here is a fragment of code that is responsible for mapping data onto entities:

    $i = 0;
    $x = 0;
    foreach ($bestellerLists[0] as $bestsellerList) {
        $List[$i] = new BestsellerList();
        foreach ($bestsellerList['books'] as $book) {
            $Book = new Book();
            $bookForm = $this->bookForm;
            $bookForm->setData($Book);
            $bookForm->submit($book);
            $List[$i]->addBook($Book);
            unset($bookForm);
            unset($Book);
            $x = $x + 1;
        }
        $i = $i+ 1;
    }

The problem that I cannot solve is that in the first iteration if the inner foreach loop, everything is well, entities get mapped, the first $Book is added to the current $List.

However, the second interation of the inner loop errors out with:

You cannot change the data of a submitted form.

As you can see, the $bookForm is assigned the value with $bookForm = $this->bookForm. At the end of the current iteration I unset $bookForm in order to create a new one at the next iteration.

I really do not get why Symfony would complain about changing the data of a submitted form. A new empty form should be used at each iteration. Any idea what I might be missing here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?