Symfony2 passing values to collection form type

前端 未结 3 1060
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-02 16:52

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
相关标签:
3条回答
  • 2021-01-02 17:23

    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);
        },
    }
    
    0 讨论(0)
  • 2021-01-02 17:30

    in symfony3 :

    $builder->add('example', CollectionType::class, array(
        'entry_type'   => ExampleType::class,
        'entry_options'  => array(
            'my_custom_option'  => true),
    ));
    
    0 讨论(0)
  • 2021-01-02 17:38

    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)

    0 讨论(0)
提交回复
热议问题