dimanche 1 mars 2015

Symfony collection field type within a collection field type: add / remove elements

i've got a problem regarding the symfony collection field type. Working with one collection type (adding, removing items) is clear due to the cookbook entry of symfony for working with collection field type. For me, the problem appears when i want to add / remove items to a collection type within a collection type. Example: I have an addresses collection that gets added to my Contact Type class



public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('addresses', 'collection', array(
'type' => new AddressType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
))
;
}


This addresses collection gets as type "AddressType", which looks the following:



public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// name = country
->add('name', 'text')
->add('cities', 'collection', array(
'type' => 'text',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
))
;
}


My goal is now to be able to add / remove new "addresses" to the form (which is working due to the cookbook entry), BUT for each address, i also want to add / remove cities.


Question: When following the example of the symfony page, i should add in the jquery:



/ addresses
$collectionHolder = $('#ecommerce_user_contact_addresses');


the collection holder. For the addresses, i know what to write here because the data-prototype generated in the widget in the view has that id. But what should I write here for the inner collection? When i write:



// cities
$collectionHolderCities = $('#ecommerce_user_contact_addresses___name___cities');


, which also comes from the data-prototype of the parent div that describes in this case the "cities" element structure", i don't know now how to add / remove new elements of that.


When calling:



var prototype = $collectionHolderCities.data('prototype');


in my 'addCityForm(..)' method, the console logs me the variable 'prototype' as undefined. When i logged the prototype in the other method 'addAddressForm', it correctly showed me the prototype structure.


What should i define as collection holder of the "inner collection" for the cities, and what do i need to change in addition to get this working ?



  • Address (with country name)

    • City

    • City

    • City



  • Address (with country name)

    • City

    • City ...




Regards.


Aucun commentaire:

Enregistrer un commentaire