[英]How to make NelmioApiDocBundle work with Symfony's FormType
[英]Symfony change service scope to prototype does not work on formtype services
我将表单用作服务(也用于表单和“自定义表单类型”)。 在我的表单之一中,我有两个字段,这是两个自定义FormType
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('primary', 'shamp_searchable', [
'class' => '\Udg\CoreBundle\Entity\Ingredients\Ingredient',
'source' => $this->router->generate('adminIngredient_search'),
])
->add('secondaries', 'shamp_searchable', [
'class' => '\Udg\CoreBundle\Entity\Ingredients\Ingredient',
'source' => $this->router->generate('adminIngredient_search'),
'multiple' => true,
])
...
}
而且由于某些原因,我需要两个shamp_searchable
表单类型的实例不同。因此,在service.yml
我将范围更改为prototype
因为我们可以在文档中读取
shamp.form.shamp_searchable_:
class: Shamp\CoreBundle\Form\Type\SearchableOneToManyType
scope: prototype
arguments:
- @shamp.form.shamp_searchable_onetomany.data_transformer
tags:
- { name: form.type, alias: shamp_searchable }
shamp.form.shamp_searchable.data_transformer:
class: Shamp\CoreBundle\Form\DataTransformer\SearchableOneToManyDataTransformer
scope: prototype
arguments:
- @doctrine.orm.entity_manager
但是最后它并没有给我两个不同的实例(已通过spl_object_hash
检查)
我的问题是,您是否知道如何获取不同的实例?
使用{ name: form.type, alias: shamp_searchable }
标记时,您基本上在FormType
数组中添加了一个条目,该条目将传递给Form组件。
初始化此数组时,仅从DIC请求一次服务: 当Form组件需要您的FormType
,它将仅通过别名从此数组中检索它。
解决方案,您必须创建一个可以创建shamp表单类型的工厂服务:
/**
* @param ShampSearchableFormTypeFactory $shampFactory
*/
public function __contruct(ShampSearchableFormTypeFactory $shampFactory)
{
$this->shampFactory = $shampFactory;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('primary', $this->shampFactory->createFormType(), [
'class' => '\Udg\CoreBundle\Entity\Ingredients\Ingredient',
'source' => $this->router->generate('adminIngredient_search'),
])
->add('secondaries', $this->shampFactory->createFormType(), [
'class' => '\Udg\CoreBundle\Entity\Ingredients\Ingredient',
'source' => $this->router->generate('adminIngredient_search'),
'multiple' => true,
])
...
}
编辑:重复标签不起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.