提供参数给子窗体多数民众赞成从父窗体的服务? [英] Supplying arguments to child form thats a service from a parent form?
问题描述
我正在尝试通过为其提供参数来使子表单更具动态性(除了已经通过将其作为服务而高度可重用).
I'm trying to make a child form more dynamic (besides already being highly reusable by making it a service) by providing parameters to it.
但是,我无法为子窗体使用的选择类提供参数.
However I'm having trouble providing parameters to a choice class that the child form uses.
TL; DR:格式StateListType
继承了choice
父级.是否可以通过StateListType
中的buildForm方法更改/覆盖默认的"choice_list"?
TL;DR: The form StateListType
inherits the choice
parent. Is it possible to change/override the default 'choice_list' from the buildForm method inside StateListType
?
以下代码中的注释说明了我想做的事情.
The comments in the following code explain what I'd like to do.
<?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';
}
}
以下代码是服务容器负责为其注入适当依赖关系的一种形式.
The following code is a form that the service container takes care of injecting the proper dependency to it.
<?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
StateListChoices.php
class StateListChoices extends LazyChoiceList implements ChoiceListInterface
{
...
public function doSomething($argument)
{
//Does something with argument
return $this; //Returns ChoiceListInterface object.
}
...
}
推荐答案
如果您的buildForm()
在StateListType
中,我不知道为什么不直接在PersonType
中构建表单?
If your buildForm()
in StateListType
I don't know why not just build the form in PersonType
directly?
您还可以将PersonType
定义为服务:
You can also define PersonType
as a service:
PersonType:
class: Acme\DemoBundle\Form\PersonType
arguments: ["@StateListChoices"]
tags:
{name: form.type, alias: person}
然后直接在此处使用选择服务:
Then use the choices services there directly:
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();
));
}
这篇关于提供参数给子窗体多数民众赞成从父窗体的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!