7

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!

Mr. 14
  • 9,228
  • 6
  • 37
  • 54

3 Answers3

8

in symfony3 :

$builder->add('example', CollectionType::class, array(
    'entry_type'   => ExampleType::class,
    'entry_options'  => array(
        'my_custom_option'  => true),
));
Themer
  • 584
  • 4
  • 9
4

Use the constructor in AddressType, its works for me..

CustomerType:

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

AddressType:

private $your_variable;

public function __construct($variable)
{
    $this->your_variable= $variable;
}
...
public function buildForm(FormBuilderInterface $builder, array $options){
    $your_variable = $this->your_variable;
    'query_builder' => function(CityRepository $cr) use ($your_variable) {
        return $cr->getCityQB($your_variable);
    },
}
Andy.Diaz
  • 3,369
  • 2
  • 22
  • 24
  • 4
    This solution is deprecated in Symfony 2.7 and can't be possible in 3.0. You can't pass parameters in Class formType now, but you can do it by inserting nex element in $options : http://stackoverflow.com/a/34035653/1338420 – Benji_X80 Jan 22 '16 at 12:18
3

I think you could use the 'options' option of the collection type. It's better than using the constructor in case you want to reuse the form elsewhere.

Symfony Form Reference: Collection Type

But remember to define the variable in your setDefaultOptions method. (Both forms must have it)

JhovaniC
  • 304
  • 3
  • 17
  • 1
    This "options" option works also for custom defined types, extending AbstractType. Thanks for the clue! – Veelkoov Jun 18 '14 at 09:58