Forms 向父窗体中的服务子窗体提供参数?
我试图通过向子窗体提供参数,使其成为一种服务,从而使其具有更高的可重用性,同时使其更具动态性 但是,我无法为子窗体使用的choice类提供参数 TL;DR:表单StateListType继承父选项。是否可以在StateListType中更改/覆盖buildForm方法中的默认“choice_list” 下面代码中的注释解释了我想做什么Forms 向父窗体中的服务子窗体提供参数?,forms,symfony,Forms,Symfony,我试图通过向子窗体提供参数,使其成为一种服务,从而使其具有更高的可重用性,同时使其更具动态性 但是,我无法为子窗体使用的choice类提供参数 TL;DR:表单StateListType继承父选项。是否可以在StateListType中更改/覆盖buildForm方法中的默认“choice_list” 下面代码中的注释解释了我想做什么 <?php namespace Acme\DemoBundle\Form\Type; use Symfony\Component\Form\Abstra
<?php
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PersonFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('State', 'statelist'),//statelist is a form service.
->add('HomeState', 'statelist')//use form service here too
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Person'
));
}
public function getName()
{
return 'person';
}
}
自定义选择类:
StateListChoices.php
class StateListChoices extends LazyChoiceList implements ChoiceListInterface
{
...
public function doSomething($argument)
{
//Does something with argument
return $this; //Returns ChoiceListInterface object.
}
...
}
如果你在StateListType中构建表单,我不知道为什么不直接在PersonType中构建表单
您还可以将PersonType定义为服务:
PersonType:
class: Acme\DemoBundle\Form\PersonType
arguments: ["@StateListChoices"]
tags:
{name: form.type, alias: person}
然后直接使用那里的服务选项:
use Acme\DemoBundle\Form\Extension\StateListChoices
class PersonType extends AbstractType
{
public function __construct(ChoiceListInterface $choicesList)
{
$this->choicesList = $choicesList;
}
private function getChoices($argument = null)
{
return $this->choicesList->doSomething($argument);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('State', 'choice', array(
'choice_list' => $this->getChoices($options['custom_argument']),
))
->add('HomeState', 'statelist', array(
'choice_list' => $this->getChoices();
));
}
您是否尝试过$builder->add'State','statelist',array'custom_argument'=>'您的自定义变量'?不能这样做,因为“PersonFormType”下有另一个名为HomeState的方法。您到底想做什么?是否要将自定义状态列表传递给状态列表表单?@Ramy我已经可以通过使用选项列表的setDefaultOptions来实现。也没有调用buildForm,但是我不能从父窗体向ChoiceList提供任何参数,这就是问题所在。我刚刚注意到您正在谈论父窗体。是的,我可以传递参数,但如何让ChoiceList在StateListType表单中使用它?因为我希望能够将选项作为服务使用,而不必将其注入除PersonFormType之外的每个表单。我希望StateListChoices通过StateListType表单作为服务保留。它仍然是一个服务。有什么区别?无论如何如果你找到了你想做的,写在这里作为参考。谢谢。我把它作为一项服务加上去了。如果那是你想要的。尽管我相信它是完全一样的,但我希望能够通过父窗体生成器参数使用它,而不是让它注入我使用它的每个窗体。
PersonType:
class: Acme\DemoBundle\Form\PersonType
arguments: ["@StateListChoices"]
tags:
{name: form.type, alias: person}
use Acme\DemoBundle\Form\Extension\StateListChoices
class PersonType extends AbstractType
{
public function __construct(ChoiceListInterface $choicesList)
{
$this->choicesList = $choicesList;
}
private function getChoices($argument = null)
{
return $this->choicesList->doSomething($argument);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('State', 'choice', array(
'choice_list' => $this->getChoices($options['custom_argument']),
))
->add('HomeState', 'statelist', array(
'choice_list' => $this->getChoices();
));
}