我使用entity
表单类型来提供表单中的Position
实体列表。我经常使用它(每个都有相同的“设置”代码来定制它),我决定从中为它创建一个自定义表单类型,以便更好地重用。Symfony2:自定义表单类型的动态默认值?
下面是当前表单类型:
class PositionType extends AbstractType
{
private $om;
public function __construct(ObjectManager $om, $mode)
{
$this->om = $om;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
// I need to pass "mode" as an option when building the form.
$mode = ???
$query_builder = function (EntityRepository $em) use ($mode) {
// Limit the positions returned based on the editing mode
return $em
->createQueryBuilder('Position')
->orderBy('Position.name')
->leftJoin('Position.type', 'Type')
->andWhere('Type.id IN (:ids)')
->setParameter('ids', Type::typesForMode($mode))
;
};
$resolver
->setRequired(array('mode'))
->setDefaults(array(
'label' => 'Position',
'class' => 'AcmeBundle:Position',
'property' => 'name',
'query_builder' => $query_builder,
'empty_value' => '',
'empty_data' => null,
'constraints' => array(
new NotBlank(),
),
))
;
}
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'position';
}
}
不要担心在查询生成器的细节,这并不重要。重要的部分是我试图在查询生成器中使用表单类型选项。
我该怎么做?问题是我不能在setDefaultOptions
中使用$mode
(我想通过的选项来更改查询生成器)。
我开始寻找一种方法从buildForm
中设置查询生成器,但我不确定我能做到这一点。
完美,正是我所需要的。谢谢。 – Brian 2014-09-19 17:20:18
虽然我不是通过实体管理器,而是像最初一样返回可调用的函数,它通过Symfony – Brian 2014-09-19 17:21:49
的管理器正确地:)并欢迎您;) – 2014-09-19 18:57:58