简体   繁体   中英

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 .

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

The FormEvents::PRE_SET_DATA event is dispatched at the beginning of the Form::setData() method. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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