如何通过在`collection`领域的Symfony 2.1选项CustomType?(How t

2019-08-17 17:03发布

我有SuperType表格实体Super

在这种形式我有一个collection领域ChildType形态类型实体Child

class SuperType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('childrens', 'collection', array(
            'type' => new ChildType(null, array('my_custom_option' => true)),  
}

class ChildType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if ($options['my_custom_option']) {
        $builder->add('my_custom_field', 'textarea'));
    }
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
  $resolver->setDefaults(array(
      ...
      'my_custom_option' => false
  ));
}

我怎样才能改变my_custom_option仅此值SuperType形式?

当然,我已经试过路过通过构造这个选项不起作用。

Answer 1:

你可以传递一个选项数组如下您childType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('childrens', 'collection', array(
            'entry_type' => new ChildType(),  
            'entry_options'  => array(
                'my_custom_option' => true,
            ),
    // ...

}


Answer 2:

在Symfony的3,这就是所谓的entry_options 。

$builder->add('childrens', CollectionType::class, array(
    'entry_type'   => ChildType::class,
    'entry_options'  => array(
        'my_custom_option'  => true
    ),
));


文章来源: How to pass options to CustomType in `collection` field Symfony 2.1?