简体   繁体   中英

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

After adding them to your choice within bind() , unset them from the passed $taintedValues array so CSRF validates.

Then override doSave() to grab them from the form object or defer to saving the embedded form where it does something similar.

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.

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