Advanced usage: example with Doctrine

That's not really an advanced topic, but the sample will be a bit hard to read (as it takes many files), so let's say it targets more experienced Symfony users.

In this demo, we're displaying a MyArray of MyElements, both persisted into the database. The MyArray entity only have an id and a name, and MyElement has id, MyArray's id and a value. This is quite the same as we seen in all demos, but persisted.


Existing arrays:
Name Open Delete
___ ☘ Cela fonctionne ☘ Open Delete
___ ☘ Cela fonctionne ☠Open Delete
___ ☘ Cela fonctionne ☘ Open Delete
_____ Open Delete
___m Open Delete
__bvnvbnvbnvbn Open Delete
_Greeting, :D happy 2018 Open Delete
--- Open Delete
---#i21ef661e2506d5d0e513dce166c17d49 Open Delete
---#i27992875ec4286a24e569b1e015b0473 Open Delete
---#i5784b3886628c4362594ffa43547edb4 Open Delete
---#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
---#ie025efbbb0738ed01a63b326c174233e Open Delete
? Open Delete
' Open Delete
() Open Delete
(orphée) Open Delete
(orphée) Open Delete
#72uuuu Open Delete
#91 Open Delete
%2372 Open Delete
%237233333 Open Delete
%2372uuuu Open Delete
%25237233333#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
%25237233333#i21ef661e2506d5d0e513dce166c17d49 Open Delete
%25237233333#i27992875ec4286a24e569b1e015b0473 Open Delete
%25237233333#i5784b3886628c4362594ffa43547edb4 Open Delete
%25237233333#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
%25237233333#ie025efbbb0738ed01a63b326c174233e Open Delete
%252372uuuu Open Delete
%252372uuuu#i27992875ec4286a24e569b1e015b0473 Open Delete
%252372uuuu#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
%252391 Open Delete
%253F#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
%253F#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
%253F#i27992875ec4286a24e569b1e015b0473 Open Delete
%253F#i5784b3886628c4362594ffa43547edb4 Open Delete
%253F#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
%253F#ie025efbbb0738ed01a63b326c174233e Open Delete
%3F Open Delete
<script>alert('test')</script> Open Delete
<script>alert('test')</script>%n%n%n%n%n%n Open Delete
<script>alert('test')</script>%n%n%n%n%n%n<script>alert('test')</script>%n%n%n%n%n%n<script>alert('test')</script>%n%n%n%n%n%n<script>alert('test')</script>%n%n%n%n%n%n<script>alert('test')</script>%n%n%n%n%n%n<script>alert('test')</script>%n%n%n%n%n%n<sc Open Delete
000tom Open Delete
11 Open Delete
11#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
11#i21ef661e2506d5d0e513dce166c17d49 Open Delete
11#i27992875ec4286a24e569b1e015b0473 Open Delete
11#i5784b3886628c4362594ffa43547edb4 Open Delete
11#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
11#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
11#ie025efbbb0738ed01a63b326c174233e Open Delete
1111 Open Delete
1111#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
1111#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
1111#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
1111#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
1111#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
11111 Open Delete
1111111111111111111111111111111111#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
1111111111111111111111111111111111#i21ef661e2506d5d0e513dce166c17d49 Open Delete
1111111111111111111111111111111111#i27992875ec4286a24e569b1e015b0473 Open Delete
1111111111111111111111111111111111#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
1111Yapi Open Delete
1121 Open Delete
122 Open Delete
123123 Open Delete
12345 Open Delete
1Alpha Open Delete
2222 example Open Delete
2232323 Open Delete
32 Open Delete
32jnni Open Delete
33 Open Delete
454 Open Delete
4545 Open Delete
5 Open Delete
8 Open Delete
86787 Open Delete
88 Open Delete
8899 Open Delete
8chan.org Open Delete
99 Open Delete
993223r23 Open Delete
999 Open Delete
a Lorenz Open Delete
a woop woop Open Delete
ñlkjlkj Open Delete
☘ Cela fonctionne ☘ Open Delete
ñlkjlkj Open Delete
ñsfkjaldsa Open Delete
aaa Open Delete
aaaaaaa Open Delete
тест Open Delete
aaamine22222 Open Delete
aaamine22222#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
aaamine22222#i21ef661e2506d5d0e513dce166c17d49 Open Delete
aaamine22222#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
aaamine22222#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
aarzarzample Open Delete
Abc1 Open Delete
adasdada Open Delete
adsfdsf Open Delete
alsi Open Delete
Array Open Delete
array new Open Delete
array2 Open Delete
articulo Open Delete
asas Open Delete
asd Open Delete
asda Open Delete
asdasd Open Delete
asdasd#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
asdasd#i5784b3886628c4362594ffa43547edb4 Open Delete
asdasd#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
asdasd#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
asdasd#ie025efbbb0738ed01a63b326c174233e Open Delete
asdasdadasd Open Delete
asdasdadasd#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
asdasdadasd#i27992875ec4286a24e569b1e015b0473 Open Delete
asdasdadasd#i5784b3886628c4362594ffa43547edb4 Open Delete
asdasdadasd#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
asdasdasd Open Delete
asdf Open Delete
asdfg Open Delete
b Open Delete
bc Open Delete
bjfhj#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
bjfhj#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
bjfhj#i21ef661e2506d5d0e513dce166c17d49 Open Delete
bjfhj#i27992875ec4286a24e569b1e015b0473 Open Delete
bjfhj#i5784b3886628c4362594ffa43547edb4 Open Delete
bjfhj#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
bla Open Delete
blah Open Delete
blah#i27992875ec4286a24e569b1e015b0473 Open Delete
blah#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
blah#ie025efbbb0738ed01a63b326c174233e Open Delete
Bra Open Delete
Caracas222 Open Delete
Caracas222666 Open Delete
checks Open Delete
ciao Open Delete
colombo Open Delete
cups Open Delete
cx Open Delete
cx%3Cwc Open Delete
cx<wc Open Delete
d Open Delete
ddd Open Delete
ddddd Open Delete
defrsdf Open Delete
delete_empty Open Delete
dfa#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
dfa#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
dfa#i21ef661e2506d5d0e513dce166c17d49 Open Delete
dfa#i5784b3886628c4362594ffa43547edb4 Open Delete
diego Open Delete
dsds Open Delete
dsvfdsfg] Open Delete
e Open Delete
ejemplo 2017-11-23 Open Delete
eqweqwe Open Delete
erwgfu%C4%B1gj8hl%C4%B1 Open Delete
erwgfuıgj8hlı Open Delete
erwgfuıgj8hlı Open Delete
exadasdas Open Delete
exampldfdfe Open Delete
example Delete
example123 Open Delete
example123#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
example123#i21ef661e2506d5d0e513dce166c17d49 Open Delete
example123#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
example123#ie025efbbb0738ed01a63b326c174233e Open Delete
example2 Open Delete
example33333 Open Delete
example4 Open Delete
exampleasd Open Delete
exampleasfsafasf#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
exampleasfsafasf#i27992875ec4286a24e569b1e015b0473 Open Delete
exampleasfsafasf#i5784b3886628c4362594ffa43547edb4 Open Delete
exampleasfsafasf#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
exampled Open Delete
exampledds Open Delete
exampledsdsddasds,sad,a,d,as,d,asd,asd Open Delete
examplee Open Delete
exampleede Open Delete
exampleeeeeeeeeeeee Open Delete
examplehfghfhfgfg Open Delete
examplehjkhjk Open Delete
examplemmm Open Delete
examplenhnhd Open Delete
exampleqqqq Open Delete
exampleRW Open Delete
exampleRW2 Open Delete
examplesdsd Open Delete
exampless Open Delete
exampleu Open Delete
examplew Open Delete
Existing arrays: Open Delete
exkiample Open Delete
fasdfasf Open Delete
fc Open Delete
fdasfdaF Open Delete
fdf Open Delete
fdf#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
fdf#i5784b3886628c4362594ffa43547edb4 Open Delete
fdf#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
fdf#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
fdf#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
fdf#ie025efbbb0738ed01a63b326c174233e Open Delete
fdsfds Open Delete
ffff Open Delete
fg Open Delete
fgdfg Open Delete
fgdfgfg Open Delete
fghjk, Open Delete
foo Open Delete
Fora Bolsonaro Open Delete
gbfgbf Open Delete
gdfg#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
gdfg#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
gdfg#i27992875ec4286a24e569b1e015b0473 Open Delete
gdfg#i5784b3886628c4362594ffa43547edb4 Open Delete
gdfg#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
gege Open Delete
geht nicht Open Delete
gg Open Delete
ggg Open Delete
ghgtytyt Open Delete
ghjk Open Delete
ghjk#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
ghjk#i21ef661e2506d5d0e513dce166c17d49 Open Delete
ghjk#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
ghjk#ie025efbbb0738ed01a63b326c174233e Open Delete
ghkjgykg Open Delete
Group B Open Delete
Group C Open Delete
Group%2BC#i27992875ec4286a24e569b1e015b0473 Open Delete
Group%2BC#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
Group%2BC#ie025efbbb0738ed01a63b326c174233e Open Delete
hbhbhb Open Delete
hello Open Delete
Hello%20World%20! Open Delete
hellowrld Open Delete
HEYY :D Open Delete
HEYY%2520:D#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
HEYY%2520:D#i21ef661e2506d5d0e513dce166c17d49 Open Delete
HEYY%2520:D#i27992875ec4286a24e569b1e015b0473 Open Delete
HEYY%2520:D#i5784b3886628c4362594ffa43547edb4 Open Delete
HEYY%2520:D#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
HEYY%2520:D#ie025efbbb0738ed01a63b326c174233e Open Delete
hh Open Delete
hhh Open Delete
hi bitches Open Delete
hjvhj Open Delete
ho Open Delete
htt Open Delete
hu Open Delete
iii Open Delete
itemList2 Open Delete
jb Open Delete
jbnn Open Delete
jghjghjgj Open Delete
jtujjk Open Delete
kappa Open Delete
kfhk Open Delete
khj#i27992875ec4286a24e569b1e015b0473 Open Delete
khj#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
khj#ie025efbbb0738ed01a63b326c174233e Open Delete
kiki Open Delete
kj Open Delete
kjkjkj Open Delete
kjkk Open Delete
kkik Open Delete
kkk#i27992875ec4286a24e569b1e015b0473 Open Delete
kkk#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
kkk#ie025efbbb0738ed01a63b326c174233e Open Delete
klklklkl Open Delete
kpok´p Open Delete
l Open Delete
lj Open Delete
llllllllllllllll Open Delete
lol Open Delete
lolo Open Delete
Marcin Open Delete
mec Open Delete
Memes Open Delete
miTest Open Delete
mm Open Delete
moj Open Delete
mon exemple Open Delete
mon tableau Open Delete
mon%20exemple Open Delete
mon%20putain%20de%20tableau Open Delete
mon%20tableau Open Delete
mp Open Delete
msqdgj Open Delete
Mumbojumbo Open Delete
тест Open Delete
new Open Delete
new test Open Delete
new_22 Open Delete
New_test_2017-03_03 Open Delete
New%20Array Open Delete
new%20test Open Delete
nmn Open Delete
nmn#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
nmn#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
nmn#i21ef661e2506d5d0e513dce166c17d49 Open Delete
nmn#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
nmn#ie025efbbb0738ed01a63b326c174233e Open Delete
ñsfkjaldsa Open Delete
o Open Delete
oı Open Delete
oezjfez Open Delete
oij Open Delete
Open Delete
olaboga Open Delete
Ouiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii Open Delete
ouiou Open Delete
PC Open Delete
perebetes Open Delete
perses Open Delete
perses#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
perses#i21ef661e2506d5d0e513dce166c17d49 Open Delete
perses#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
perses#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
perses#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
perses#ie025efbbb0738ed01a63b326c174233e Open Delete
poiuytre Open Delete
polo Open Delete
pp Open Delete
pppppii Open Delete
pppppii#i5784b3886628c4362594ffa43547edb4 Open Delete
pppppii#ie025efbbb0738ed01a63b326c174233e Open Delete
prabu Open Delete
Putin Open Delete
qqq Open Delete
qsd Open Delete
question2 Open Delete
qwehgnhngh Open Delete
qweqw Open Delete
qwerqwerqw Open Delete
qwery Open Delete
qwery2 Open Delete
rar Open Delete
Recipe Open Delete
rerererere Open Delete
rerwrer Open Delete
rr Open Delete
rrrr#i27992875ec4286a24e569b1e015b0473 Open Delete
rrrr#i5784b3886628c4362594ffa43547edb4 Open Delete
rrrr#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
rrrr#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
rrrr#ie025efbbb0738ed01a63b326c174233e Open Delete
rtr Open Delete
s Open Delete
sa Open Delete
sad.asdasd.sdsad Open Delete
sadsad Open Delete
sam Open Delete
samArray Open Delete
samples Open Delete
sasa Open Delete
scts Open Delete
sd Open Delete
sdasdafs Open Delete
sdfsdf Open Delete
sdsd Open Delete
SED Open Delete
seeme#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
seeme#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
seeme#i21ef661e2506d5d0e513dce166c17d49 Open Delete
seeme#i27992875ec4286a24e569b1e015b0473 Open Delete
seeme#i5784b3886628c4362594ffa43547edb4 Open Delete
seeme#i8c18088ea4d8dd4d6e5e986bceed35da Open Delete
seeme#ibcac9f4c58199eeaa7ba5c7750b8434b Open Delete
select Open Delete
sf Open Delete
sfdf Open Delete
sfsa Open Delete
sfsdfsd Open Delete
sgdfs Open Delete
someNewArray#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
someNewArray#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
someNewArray#i5784b3886628c4362594ffa43547edb4 Open Delete
someNewArray#ic3ee8978c60737ab5d058b5a3f6a1b09 Open Delete
someNewArray#ie025efbbb0738ed01a63b326c174233e Open Delete
sqsq Open Delete
ssss Open Delete
STASI Open Delete
Stringkey Open Delete
Ta%20mere%20la%20grosse%20choin Open Delete
Ta%2520mere%2520la%2520grosse%2520choin Open Delete
Ta%2520mere%2520la%2520grosse%2520choin#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
Ta%2520mere%2520la%2520grosse%2520choin#i27992875ec4286a24e569b1e015b0473 Open Delete
Ta%2520mere%2520la%2520grosse%2520choin#i5784b3886628c4362594ffa43547edb4 Open Delete
Ta%2520mere%2520la%2520grosse%2520choin#ie025efbbb0738ed01a63b326c174233e Open Delete
TATA Open Delete
Telous Open Delete
tes Open Delete
tesr Open Delete
tesss Open Delete
tesst Open Delete
test Open Delete
test2 Open Delete
test22 Open Delete
test34 Open Delete
Teste Open Delete
Tester Open Delete
testN17 Open Delete
testname Open Delete
testsetse Open Delete
tet Open Delete
texte Open Delete
the test Open Delete
this is an array Open Delete
tojo Open Delete
toti Open Delete
toto Open Delete
try01 Open Delete
ttt Open Delete
tutu Open Delete
tyty Open Delete
ugghutugituiutu Open Delete
uhj Open Delete
ui Open Delete
uiu Open Delete
undefined Open Delete
Vai que da Open Delete
Vai%20que%20da Open Delete
vdsvd Open Delete
Volta%20Di Open Delete
Volta%2520Dilma%2520Fora%2520Temer#i1097c8b84b58a65ed6ec7b4f0da3ab67 Open Delete
Volta%2520Dilma%2520Fora%2520Temer#i1dcd1cbb0d8eeb1fd3a32dc2aa9c8013 Open Delete
Volta%2520Dilma%2520Fora%2520Temer#i2 Open Delete
w Open Delete
why doctrine on delete not work? Open Delete
why%20doctrine%20on%20delete%20not%20work%3F Open Delete
Woop wooop Open Delete
wqeqe Open Delete
wsh yannito Open Delete
wxc%3Cwxc Open Delete
wxc<wxc Open Delete
You Open Delete
z Open Delete
zizizizizi Open Delete
<?php

namespace Fuz\AppBundle\Base;

use Fuz\AppBundle\Entity\Value;
use Fuz\AppBundle\Form\ValueType;
use Fuz\QuickStartBundle\Base\BaseController as QuickStartBase;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\HttpFoundation\Request;

class BaseController extends QuickStartBase
{
    protected function createContextSample(Request $request, $name = 'form', $values = ['a', 'b', 'c'])
    {
        $data = ['values' => $values];

        $form = $this
           ->get('form.factory')
           ->createNamedBuilder($name, Type\FormType::class, $data)
           ->add('values', Type\CollectionType::class, [
               'entry_type'    => Type\TextType::class,
               'label'         => 'Add, move, remove values and press Submit.',
               'entry_options' => [
                   'label' => 'Value',
               ],
               'allow_add'    => true,
               'allow_delete' => true,
               'prototype'    => true,
               'attr'         => [
                   'class' => "{$name}-collection",
               ],
           ])
           ->add('submit', Type\SubmitType::class)
           ->getForm()
        ;

        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
        }

        return [
            $name         => $form->createView(),
            "{$name}Data" => $data,
        ];
    }

    protected function createAdvancedContextSample(Request $request, $name = 'advancedForm')
    {
        $a = new Value('a');
        $b = new Value('b');
        $c = new Value('c');

        $data = ['values' => [$a, $b, $c]];

        $form = $this
           ->get('form.factory')
           ->createNamedBuilder($name, Type\FormType::class, $data)
           ->add('values', Type\CollectionType::class, [
               'entry_type'   => ValueType::class,
               'label'        => 'Add, move, remove values and press Submit.',
               'allow_add'    => true,
               'allow_delete' => true,
               'prototype'    => true,
               'required'     => false,
               'attr'         => [
                   'class' => "{$name}-collection",
               ],
           ])
           ->add('submit', Type\SubmitType::class)
           ->getForm()
        ;

        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
        }

        return [
            $name         => $form->createView(),
            "{$name}Data" => $data,
        ];
    }
}
<?php

namespace Fuz\AppBundle\Controller;

use Fuz\AppBundle\Base\BaseController;
use Fuz\AppBundle\Entity\Address;
use Fuz\AppBundle\Entity\Addresses;
use Fuz\AppBundle\Entity\Fancy;
use Fuz\AppBundle\Entity\FancyCollection;
use Fuz\AppBundle\Entity\Value;
use Fuz\AppBundle\Form\AddressesType;
use Fuz\AppBundle\Form\FancyCollectionType;
use Fuz\AppBundle\Form\MyArrayType;
use Fuz\AppBundle\Form\ValueType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\HttpFoundation\Request;

/**
 * @Route("/advanced")
 */
class AdvancedController extends BaseController
{
    /**
     * Advanced usage
     *
     * You can reference your form collection in the view, instead of
     * putting a selector in the form type.
     *
     * @Route("/mvcCompliance", name="mvcCompliance")
     * @Template()
     */
    public function mvcComplianceAction(Request $request)
    {
        $data = ['values' => ['a', 'b', 'c']];

        $form = $this
           ->createFormBuilder($data)
           ->add('values', Type\CollectionType::class, [
               'entry_type'    => Type\TextType::class,
               'label'         => 'Add, move, remove values and press Submit.',
               'entry_options' => [
                   'label' => 'Value',
               ],
               'allow_add'    => true,
               'allow_delete' => true,
               'prototype'    => true,
               'required'     => false,
//                   'attr' => array (
//                           'class' => 'my-selector', <--- Not MVC compliant!
//                   ),
           ])
           ->add('submit', Type\SubmitType::class)
           ->getForm()
        ;

        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
        }

        return [
            'form' => $form->createView(),
            'data' => $data,
        ];
    }

    /**
     * Advanced usage
     *
     * A custom form theme helps define button's layout and positions as and where you want.
     *
     * @Route("/customFormTheme", name="customFormTheme")
     * @Template()
     */
    public function customFormThemeAction(Request $request)
    {
        $data = ['values' => [new Value('a'), new Value('b'), new Value('c')]];

        $form = $this
           ->createFormBuilder($data)
           ->add('values', Type\CollectionType::class, [
               'entry_type'   => ValueType::class,
               'label'        => 'Add, move, remove values and press Submit.',
               'allow_add'    => true,
               'allow_delete' => true,
               'prototype'    => true,
               'required'     => false,
               'attr'         => [
                   'class' => 'collection',
               ],
           ])
           ->add('submit', Type\SubmitType::class)
           ->getForm()
        ;

        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
        }

        return [
            'form' => $form->createView(),
            'data' => $data,
        ];
    }

    /**
     * Advanced usage
     *
     * Same demo as above, but with the add button located at the bottom of
     * the form instead of close to each field.
     *
     * @Route("/customFormThemeAddBottom", name="customFormThemeAddBottom")
     * @Template()
     */
    public function customFormThemeAddBottomAction(Request $request)
    {
        return $this->customFormThemeAction($request);
    }

    /**
     * Advanced usage
     *
     * Collection of collections are useful on the most dynamic forms, and a good
     * way to test if the plugin is working as expected too.
     *
     * @Route("/collectionOfCollections", name="collectionOfCollections")
     * @Template()
     */
    public function collectionOfCollectionsAction(Request $request)
    {
        $data = [
            'collections' => [
                [new Value('a'), new Value('b'), new Value('c')],
                [new Value('d'), new Value('e'), new Value('f')],
                [new Value('g'), new Value('h'), new Value('i')],
            ],
        ];

        $form = $this
           ->get('form.factory')
           ->createNamedBuilder('form', Type\FormType::class, $data)
           ->add('collections', Type\CollectionType::class, [
               'entry_type'    => Type\CollectionType::class,
               'label'         => 'Add, move, remove collections',
               'entry_options' => [
                   'entry_type'    => ValueType::class,
                   'label'         => 'Add, move, remove values',
                   'entry_options' => [
                       'label' => 'Value',
                   ],
                   'allow_add'      => true,
                   'allow_delete'   => true,
                   'prototype'      => true,
                   'prototype_name' => '__children_name__',
                   'attr'           => [
                       'class' => 'child-collection',
                   ],
               ],
               'allow_add'      => true,
               'allow_delete'   => true,
               'prototype'      => true,
               'prototype_name' => '__parent_name__',
               'attr'           => [
                   'class' => 'parent-collection',
               ],
           ])
           ->add('submit', Type\SubmitType::class)
           ->getForm()
        ;

        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
        }

        return [
            'form' => $form->createView(),
            'data' => $data,
        ];
    }

    /**
     * Advanced usage
     *
     * Looks like there are weird behaviours with Doctrine:
     * https://github.com/ninsuo/symfony-collection/issues/7
     * Let's test that live!
     *
     * ... hmm, doesn't look bad anyway
     *
     * @Route(
     *      "/usageWithDoctrine/{name}",
     *      name = "usageWithDoctrine",
     *      defaults = {
     *          "name" = "example"
     *      }
     * )
     * @Template()
     */
    public function usageWithDoctrineAction(Request $request, $name)
    {
        $repo = $this->getDoctrine()->getRepository('FuzAppBundle:MyArray');

        $data = $repo->findOneByName($name);
        if (is_null($data)) {
            $data = $repo->create($name);
        }

        $form = $this->createForm(MyArrayType::class, $data);
        $form->handleRequest($request);

        $form->get('save')->isClicked() && $form->isValid() && $repo->save($data);

        return [
            'names' => $repo->getArrayNames(),
            'form'  => $form->createView(),
            'data'  => $data,
        ];
    }

    /**
     * Related to usageWithDoctrine demo
     *
     * @Route(
     *      "/usageWithDoctrineDelete/{name}",
     *      name = "usageWithDoctrineDelete"
     * )
     */
    public function usageWithDoctrineDeleteAction(Request $request, $name)
    {
        $repo = $this->getDoctrine()->getRepository('FuzAppBundle:MyArray');
        if (!is_null($data = $repo->findOneByName($name))) {
            $repo->delete($data);
        }

        return $this->forward('FuzAppBundle:Advanced:usageWithDoctrine', [
            'name' => 'example',
        ]);
    }

    /**
     * A form having a theme and containing several fields
     *
     * @Route(
     *      "/formHavingSeveralFields",
     *      name = "formHavingSeveralFields"
     * )
     * @Template()
     */
    public function formHavingSeveralFieldsAction(Request $request)
    {
        $address = new Address();
        $address->setName('Mickael Steller');
        $address->setCompany('fuz.org');
        $address->setStreet('41 rue de la Paix');
        $address->setPostalcode('75002');
        $address->setCity('Paris');
        $address->setCountry('France');

        $addresses = new Addresses();
        $addresses->getAddresses()->add($address);

        $form = $this->createForm(AddressesType::class, $addresses);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);
        }

        return [
            'form' => $form->createView(),
            'data' => $addresses,
        ];
    }

    /**
     * Another example of form theme
     *
     * @Route(
     *      "/fancyFormTheme",
     *      name = "fancyFormTheme"
     * )
     * @Template()
     */
    public function fancyFormThemeAction(Request $request)
    {
        $fancyCollection = new FancyCollection();

        for ($i = 0; $i < 3; $i++) {
            $fancy = new Fancy();
            $fancyCollection->getFancyCollection()->add($fancy);
        }

        $form = $this->createForm(FancyCollectionType::class, $fancyCollection);

        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
        }

        return [
            'form' => $form->createView(),
            'data' => $fancyCollection,
        ];
    }
}
<?php

namespace Fuz\AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * MyArray
 *
 * @ORM\Table(
 *      name="my_array",
 *      uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}
 * )
 * @ORM\Entity(repositoryClass="Fuz\AppBundle\Repository\MyArrayRepository")
 */
class MyArray
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="MyElement", mappedBy="array", cascade={"all"}, orphanRemoval=true)
     */
    protected $elements;

    public function __construct()
    {
        $this->elements = new ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    public function getElements()
    {
        return $this->elements;
    }
}
<?php

namespace Fuz\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MyElement
 *
 * @ORM\Table(name="my_element"),
 * @ORM\Entity
 */
class MyElement
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var MyArray
     *
     * @ORM\ManyToOne(targetEntity="MyArray", inversedBy="elements")
     * @ORM\JoinColumn(name="my_array_id", referencedColumnName="id")
     */
    protected $array;

    /**
     * @var string
     *
     * @ORM\Column(name="value", type="string", length=255)
     */
    protected $value;

    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setArray(MyArray $array)
    {
        $this->array = $array;

        return $this;
    }

    public function getArray()
    {
        return $this->array;
    }

    public function setValue($value)
    {
        $this->value = $value;

        return $this;
    }

    public function getValue()
    {
        return $this->value;
    }
}
<?php

namespace Fuz\AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MyArrayType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType::class, [
                'label' => 'Array name:',
        ]);

        $builder->add('elements', CollectionType::class, [
            'label'        => 'Add an element...',
            'entry_type'   => MyElementType::class,
            'allow_add'    => true,
            'allow_delete' => true,
            'prototype'    => true,
            'required'     => false,
            'by_reference' => true,
            'delete_empty' => true,
            'attr'         => [
                'class' => 'doctrine-sample',
            ],
        ]);

        $builder->add('save', SubmitType::class, [
                'label' => 'Save this array',
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'Fuz\AppBundle\Entity\MyArray',
        ]);
    }

    public function getBlockPrefix()
    {
        return 'my_array';
    }
}
<?php

namespace Fuz\AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MyElementType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('value', TextType::class, [
            'required' => false,
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'Fuz\AppBundle\Entity\MyElement',
        ]);
    }

    public function getBlockPrefix()
    {
        return 'my_element';
    }
}
<?php

namespace Fuz\AppBundle\Repository;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityRepository;
use Fuz\AppBundle\Entity\MyArray;

/**
 * MyArrayRepository.
 */
class MyArrayRepository extends EntityRepository
{
    public function create($name)
    {
        $data = new MyArray();
        $data->setName($name);
        try {
            $this->_em->persist($data);
            $this->_em->flush();
        } catch (UniqueConstraintViolationException $e) {
            return null;
        }

        return $data;
    }

    public function save(MyArray $data)
    {
        foreach ($data->getElements() as $element) {
            $element->setArray($data);
        }
        try {
            $this->_em->persist($data);
            $this->_em->flush();
        } catch (UniqueConstraintViolationException $e) {
            return null;
        }

        return $data;
    }

    public function getArrayNames()
    {
        $arrays = $this
            ->_em
            ->createQuery("
               SELECT arr.name
               FROM Fuz\AppBundle\Entity\MyArray arr
            ")
            ->execute();
        $names = [];
        foreach ($arrays as $array) {
            $names[] = $array['name'];
        }

        return $names;
    }

    public function delete(MyArray $data)
    {
        foreach ($data->getElements() as $element) {
            $this->_em->remove($element);
        }
        $this->_em->remove($data);
        $this->_em->flush();
    }
}

{% block my_element_label %}{% endblock %}
{% block my_element_errors %}{% endblock %}

{% block my_element_widget %}

    <div class="row">
        <div class="col-md-7">
            {{ form_widget(form.value) }}
        </div>
        <div class="col-md-2">
            <a href="#" class="collection-up btn btn-default">Up</a>
            <a href="#" class="collection-down btn btn-default">Down</a>
        </div>
        <div class="col-md-2">
            <a href="#" class="collection-remove btn btn-default">Remove</a>
            <a href="#" class="collection-add btn btn-default">Add</a>
        </div>
        <div class="col-md-1">
            <a href="#" class="collection-duplicate btn btn-default">Duplicate</a>
        </div>
    </div>

{% endblock %}
{% extends 'FuzAppBundle::layout.html.twig' %}

{% block extra_js %}
    <script src="{{ asset('js/jquery.collection.js') }}"></script>
{% endblock %}

{% block title %}Advanced usage: example with Doctrine{% endblock %}

{% block body %}

    <h2>{{ block('title') }}</h2>

    <p>
        That's not really an <i>advanced</i> topic, but the sample will be a bit hard to read (as it takes many files),
        so let's say it targets more experienced Symfony users.
    </p>

    <p>
        In this demo, we're displaying a MyArray of MyElements, both persisted into the database. The MyArray entity
        only have an id and a name, and MyElement has id, MyArray's id and a value. This is quite the same as we seen
        in all demos, but persisted.
    </p>

    <div class="text-center">
        <input type="button" id="new" class="btn btn-default" value="Create new array"/>
    </div>

    {%
        form_theme form
            'jquery.collection.html.twig'
            'FuzAppBundle:Advanced:doctrine-theme.html.twig'
    %}
    {{ form(form) }}

    <hr/>

    {% if names|length %}
        Existing arrays:
        <table class="table">
            <thead>
                <th>Name</th>
                <th>Open</th>
                <th>Delete</th>
            </thead>
            <tbody>
                {% for name in names %}
                    <tr>
                        <td>{{ name }}</td>
                        <td>
                            {% if data.name != name %}<a href="{{ path('usageWithDoctrine', {'name': name}) }}">Open</a>{% endif %}
                        </td>
                        <td><a href="{{ path('usageWithDoctrineDelete', {'name': name}) }}">Delete</a></td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endif %}

    {{
        tabs([
            'Base/BaseController.php',
            'Controller/AdvancedController.php',
            'Resources/views/Advanced/usageWithDoctrine.html.twig',
            'Resources/views/Advanced/doctrine-theme.html.twig',
            'Entity/MyArray.php',
            'Form/MyArrayType.php',
            'Entity/MyElement.php',
            'Form/MyElementType.php',
            'Repository/MyArrayRepository.php',
        ])
    }}

{% endblock %}

{% block script %}

    <script type="text/javascript">

        // only useful for the "new" button...
        $('#new').on('click', function() {
           var name = prompt("Which name would you like to use?");
           if (name) {
               var uri = '{{ path('usageWithDoctrine', {'name': 'a'}) }}';
               document.location = uri.substr(0, uri.length - 1) + encodeURIComponent(name);
           }
        });

        $('.doctrine-sample').collection();

    </script>

{% endblock %}