25

我有SuperType实体表格Super

在这个表单中,我有一个实体collection的表单类型字段ChildTypeChild

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表单的值?

当然,我尝试通过构造函数传递此选项的方法不起作用。

4

2 回答 2

38

您可以将一组选项传递给您的 childType,如下所示:

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

}
于 2013-03-18T14:27:13.457 回答
13

在 Symfony 3 中,这称为entry_options

$builder->add('childrens', CollectionType::class, array(
    'entry_type'   => ChildType::class,
    'entry_options'  => array(
        'my_custom_option'  => true
    ),
));
于 2016-10-19T10:04:11.717 回答