我试图按照http://symfony.com/doc/2.8/form/create_form_type_extension.html
扩展Symfony 2.8表单类型在我的ObjectType
表单中,我有类似的内容,我在其中创建表单。我的表单包含MyExtendedType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$passed_options = array('aKey'=>'aValue', ...);
$builder
->add('someotherfield', 'SomeOtherStandardType', ..)
->add('fieldname', 'AppBundle\Form\Type\MyExtendedType', $passed_options)
->add('someotherfield', 'SomeOtherStandardType', ..)
//...
;
}
在我的MyExtendedType
中,我想在fieldname
FormEvents::PRE_SET_DATA
public function getParent()
{
return 'Symfony\Bridge\Doctrine\Form\Type\EntityType';
}
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder, $options) {
$form = $event->getForm()->getParent();
$modified_options = $options; //this is not correct because there are all the resolved options of the field
$modified_options['aKey'] = 'anotherValue';
$form->add($builder->getName(), 'Symfony\Bridge\Doctrine\Form\Type\EntityType', $modified_options);
});
}
问题在于MyExtendedType
我只需要访问$passed_options
。在$options
我有从Form组件初始化的所有已解析选项,并且作为前一代码执行该字段未正确构建,因为存在彼此对比的选项(例如query_builder,选项, choice_list ..)。
以下是问题:
如何使用Form组件仅访问$passed_options
?我不想在ObjectType
中设置属性,以便采用通用方法。