但是,我遇到了几个问题:
1)我可以找到将表单字段添加到管理员中正确的’formGroup’的唯一方法是添加新字段两次(一次通过formMapper,一次通过表单本身)…这似乎非常错误,我无法控制formGroup中出现的位置.
2)添加的元素似乎不知道它有一个连接的admin(可能是因为它是使用Form :: add()添加的).这意味着,除其他外,它呈现与表单中的其他字段不同,因为它在form_admin_fIElds.HTML.twig中触发{%if如果sonata_admin未定义,或者不是sonata_admin_enabled或者不是sonata_admin.fIEld_description%}条件
所以,这让我相信我做错了,必须有更好的方法.
所以…
在使用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',$formOptions) ; }); $formMapper ->with('MyFormGroup') ->add('fIEldOne') ->add('fIEldTwo') ->end() ;}
目的是在MyFormGroup中的fIEldOne和fIEldTwo之间添加新的foo字段.
编辑:这是我在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',$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() ;}解决方法 没有时间在这里做一个长时间的回答,我会粘贴一段代码,我不知道是否完全适合你的情况,在我的它是多依赖选择(国家,州,城市,邻居)的一部分.
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',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);}总结
以上是内存溢出为你收集整理的表单 – 使用FormEvents自定义SonataAdmin中的字段的正确方法全部内容,希望文章能够帮你解决表单 – 使用FormEvents自定义SonataAdmin中的字段的正确方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)