如何在“collection”字段Symfony 2.1中将选项传递给CustomType? [英] How to pass options to CustomType in `collection` field Symfony 2.1?

查看:109
本文介绍了如何在“collection”字段Symfony 2.1中将选项传递给CustomType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 SuperType 实体表单超级



在这种形式中,我有一个集合字段 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)),
}
/ pre>

class ChildType:



$

$($ options ['my_custom_option']){
$ builder-> add('my_custom_field','textarea'));
}
}

public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(a rray(
...
'my_custom_option'=>假
));
}

如何更改 my_custom_option value only for $ SuperType form?



当然,我试过传递这个选项通过构造函数不起作用。

解决方案

您可以传递选项数组到您的childType如下,

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

}


I have SuperType Form for Entity Super.

In this form I have a collection field of ChildType Form types for Entity 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
  ));
}

How can I change the my_custom_option value only for this SuperType form?

Of course, what I've tried passing this option via constructor doesn't work.

解决方案

You can pass an array of options to your childType as follow,

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

}

这篇关于如何在“collection”字段Symfony 2.1中将选项传递给CustomType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆