Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 passing values to collection form type

Tags:

php

symfony

I have the following entity relations:

  • A Customer has one-to-many Address
  • An Address has many-to-one County and many-to-one City
  • A County has one-to-many City.

So, in my CustomerType, I have

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

And in my AddressType, I have

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ...
        ->add('city', 'entity', array(
            'class' => 'MyCustomerBundle:City',
            'query_builder' => function(CityRepository $cr) use ($options) {
                return $cr->getCityQB($options['county']);
            },
            'property' => 'city',
            'empty_value' => '',
        ))
    ;
}

My goal is to only display the set of cities for their corresponding county. I can get the values into CustomerType from $options but how can I pass down the values to AddressType? So that each address gets its corresponding county to look up the cities?

Any help would be appreciated. Thanks!

like image 259
Mr. 14 Avatar asked Mar 04 '13 10:03

Mr. 14


1 Answers

in symfony3 :

$builder->add('example', CollectionType::class, array(
    'entry_type'   => ExampleType::class,
    'entry_options'  => array(
        'my_custom_option'  => true),
));
like image 183
Themer Avatar answered Sep 24 '22 21:09

Themer