我有一个包含一些表单字段的Sonata Admin类,但我想使用FormEvents::PRE_SET_DATA
事件根据绑定数据动态添加另一个表单字段。
但是,我遇到了几个问题:
1)我能找到将表单字段添加到正确的' formGroup'的唯一方法。管理员是通过两次添加新字段(一次通过formMapper,一次通过表单本身)...这似乎非常错误,我无法控制它在formGroup中的位置。
2)添加的元素似乎并不知道它有一个连接的Admin(可能是因为它是使用Form::add()
添加的)。这意味着,除了其他方面,它呈现的形式与表单中的其他字段不同,因为它触发了{% if sonata_admin is not defined or not sonata_admin_enabled or not sonata_admin.field_description %}
form_admin_fields.html.twig
条件
所以,这让我相信我做错了,必须有更好的方法。
因此...
使用SonataAdmin时,使用FormEvent向表单组添加字段的理想方法是什么,理想情况是在该组中的首选位置?
这里有一些代码,FWIW ......
protected function configureFormFields(FormMapper $formMapper)
{
$admin = $this;
$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($admin, $formMapper) {
$subject = $event->getData();
// do something fancy with $subject
$formOptions = array(/* some cool stuff*/);
// If I don't add the field with the $formMapper then the new field doesn't appear on the rendered form
$formMapper
->with('MyFormGroup')
->add('foo', null, $formOptions)
->end()
;
// If I don't add the field with Form::add() then I get a Twig Exception:
// Key "foo" for array with keys "..." does not exist in my_form_template.html.twig at line xx
$event
->getForm()
->add('foo', null, $formOptions)
;
});
$formMapper
->with('MyFormGroup')
->add('fieldOne')
->add('fieldTwo')
->end()
;
}
目的是在foo
中的fieldOne
和fieldTwo
之间添加新的MyFormGroup
字段。
编辑:这是我在Cassiano的回答中提出的答案
protected function configureFormFields(FormMapper $formMapper)
{
$builder = $formMapper->getFormBuilder();
$ff = $builder->getFormFactory();
$admin = $this;
$formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($ff, $admin) {
$subject = $event->getData();
// do something fancy with $subject
$formOptions = array(
'auto_initialize' => false,
'class' => 'My\ProjectBundle\Entity\MyEntity',
/* some cool stuff*/
);
$event->getForm()->add($ff->createNamed('foo', 'entity', null, $formOptions));
});
$formMapper
->with('MyFormGroup')
->add('fieldOne')
->add('foo') // adding it here gets the field in the right place, it's then redefined by the event code
->add('fieldTwo')
->end()
;
}
答案 0 :(得分:3)
我没时间回答很长的答案,我会粘贴一段代码,而且我不知道是否完全适合你的情况,在我的多重依赖选择(国家,州,城市,邻居)。
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('myfield');
$builder = $formMapper->getFormBuilder();
$ff = $builder->getFormFactory();
$func = function (FormEvent $e) use ($ff) {
$form = $e->getForm();
if ($form->has('myfield')) {
$form->remove('myfield');
}
$form->add($ff->createNamed('myfield', 'entity', null, array(
'class' => '...',
'attr' => array('class' => 'form-control'),
'auto_initialize' => false,
'query_builder' => function (EntityRepository $repository) use ($pais) {
$qb = $repository->createQueryBuilder('estado');
if ($pais instanceof ...) {
$qb = $qb->where('myfield.other = :other')
->setParameter('other', $other);
} elseif(is_numeric($other)) {
$qb = $qb->where('myfield.other = :other_id')
->setParameter('other_id', $other);
}
return $qb;
}
)));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
$builder->addEventListener(FormEvents::PRE_BIND, $func);
}