JavaScript code to deal with Symfony form collections

来源:互联网 时间:1970-01-01

The cookbook

If you don’t know what form collections are you won’t be interested inthis article now. However you can read the cookbookout of curiosity and come back read this article later.

Doing form collections with Symfony can be a tedious task becauseSymfony doesn’t (and shouldn’t) embed the JavaScript to have a basicbut working form collections out of the box.

In order to make it work you have to “copy” and adapt some JavaScriptfrom the cookbook. Youmay even be tempted to duplicate this JavaScript code accross your project.

A simple JavaScript file to the rescue

Therefore I wrote a single JavaScript file to help regarding thismatter. You can find it here. Inthis repositorythere is a complete documentationand some examples.

The JavaScript file relies on html classes to enable the defaultbehavior of adding and deleting elements to/from your collection thusyour work is to add the classes in your template files. For the mostsimple cases you won’t need to write a single line of JavaScript.

The JavaScript code throws events before and after each action to letyou customize each behavior. Each class or event name can beconfigured to your need at different levels.

Despite the customization options, if you have to implement complexbehaviors, this file may not be a solution for you. Nevertheless youcan modify it to suit your needs.

Next step

Try iton your computer.

If you’ve never done form collections with Symfony before, you reallyshould follow the cookbookin order to understand how Symfony works with form collections.

If you want to know more about what is possible, you may want to readthe documentation.