我试图通过为其提供参数来让孩子形成更具活力(除了已经通过使其成为服务而具有高度可重用性)。
但是,我无法为子表单使用的选择类提供参数。
TL; DR:表单StateListType
继承choice
父级。是否可以更改/覆盖默认的' choice_list'来自StateListType
内的buildForm方法?
以下代码中的注释解释了我想要做的事情。
<?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';
}
}
以下代码是服务容器负责为其注入适当依赖关系的表单。
<?php
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class StateListType extends AbstractType
{
protected $ChoiceList;
public function __construct(ChoiceListInterface $Choices)
{
//If I'm right I can't use another parameter
//to call from the parent form since it's already a service?
$this->ChoiceList = $Choices;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
//Don't usually need this method at all.
//However if I need to provide arguments
//I can set 'custom_argument'
//in the setDefaultOptions method below.
//Then I could use $options['custom_argument']
//from the parent form to provide arguments to
//$this->ChoiceList->doSomething($options['custom_argument')
//and then replacing the default 'choice_list' with this?
//Just not sure how or if even possible to do it from here.
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults(
array(
//Could use $arg this by getting options from the constructor.
//But since it's a service I can't supply dynamic arguments.
'choice_list' => $this->ChoiceList->doSomething($arg),//Returns itself after doing something.
'empty_value' => 'select a state',
'custom_argument' => null
)
);
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'statelist';
}
}
根据评论中的要求提供额外信息:
服务:
StateListChoice:
class: Acme\DemoBundle\Form\Extension\StateListChoices
StateListType:
class: Acme\DemoBundle\Form\StateListType
arguments: ["@StateListChoices"]
tags:
{name: form.type, alias: statelist}
自定义选择类:
StateListChoices.php
class StateListChoices extends LazyChoiceList implements ChoiceListInterface
{
...
public function doSomething($argument)
{
//Does something with argument
return $this; //Returns ChoiceListInterface object.
}
...
}
答案 0 :(得分:0)
如果您buildForm()
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();
));
}