简体   繁体   English

symfony collectiontype表单字段上的增量值

[英]Increment value on symfony collectiontype form field

I have an integertype field as part of a collectiontype in symfony 4. I have everthing working with adding and removing a new collection on my form as per https://symfonycasts.com/screencast/collections/collection-allow-delete#play . 我在symfony 4中有一个integertype字段作为collectiontype的一部分。我一直在按照https://symfonycasts.com/screencast/collections/collection-allow-delete#play在表单上添加和删除新集合。

I would like to increment the value displayed in the integer field when a new collection element is added to the form to make ir easier for the end user. 当新的收集元素添加到表单中时,我想增加显示在整数字段中的值,以使ir对于最终用户更容易。 I can't quite see how to achieve this (I can get a default value but not one that dynamically changes). 我不太清楚如何实现这一点(我可以获取默认值,但不能动态更改)。 If anybody has a hint I'd be glad to hear from you. 如果有人暗示我很高兴收到您的来信。

Thank you very much, Martyn 马丁,非常感谢

As I can see from comments under your question you solved it, but I would like to post answer for someone who comes from Google, etc... You should use Form Events described here https://symfony.com/doc/current/form/events.html 正如我从问题下的评论中看到的那样,您已解决了问题,但我想为来自Google等的人发布答案...您应使用此处所述的表单事件https://symfony.com/doc/current/ form / events.html

The FormEvents::PRE_SET_DATA event is dispatched at the beginning of the Form::setData() method. FormEvents :: PRE_SET_DATA事件在Form :: setData()方法的开头分派。 It can be used to: 它可以用于:

Modify the data given during pre-population; 修改预填充期间给出的数据; Modify a form depending on the pre-populated data (adding or removing fields dynamically). 根据预填充的数据修改表单(动态添加或删除字段)。

You can cycle through form children you want and add any value you need. 您可以循环浏览所需的子窗体并添加所需的任何值。

Keep in mind that there multiple events you can listen to, so use the one you need most. 请记住,您可以收听多个事件,因此请使用最需要的事件。

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

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