简体   繁体   English

Symfony:为1-n / nn关系添加即时的小部件吗?

[英]Symfony: add on-the-fly widgets for 1-n / n-n relations?

How to let the user add widget on a form ? 如何让用户在表单上添加小部件?

Let's say that a user can add 1 or more website. 假设用户可以添加1个或多个网站。 How to let him click on a "+" button to add a new widget in order to add a second website ? 如何让他单击“ +”按钮添加新的小部件以添加第二个网站?

Thanks 谢谢

The way I've done it is to override bind() on the form class and add the elements to a special protected property on the form object. 我这样做的方法是覆盖表单类上的bind()并将元素添加到表单对象上的特殊保护属性中。 You can also add them to an embedded form specifically for handling this dynamic number of inputs. 您也可以将它们添加到专门用于处理此动态输入数量的嵌入式表单中。 Give the embedded form a public method like addWebsite() . 给嵌入式表单一个公共方法,如addWebsite()

After adding them to your choice within bind() , unset them from the passed $taintedValues array so CSRF validates. bind() 中将它们添加到您的选择中之后,从传递的$ taintedValues数组中取消设置它们,以便CSRF进行验证。

Then override doSave() to grab them from the form object or defer to saving the embedded form where it does something similar. 然后重写doSave()以从表单对象中获取它们,或推迟到将嵌入的表单保存到类似的位置。

I haven't been able to "best practice" this, but you'll want to try the embedded form way if you have the time to tool around and the same handling is used in multiple forms. 我还没有“最佳实践”的经验,但是如果您有时间整理工具并且在多种形式中使用相同的处理方法,那么您将尝试使用嵌入式表单方式。

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

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