Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 使用FormEvents自定义SonataAdmin中字段的正确方法_Forms_Symfony_Sonata Admin - Fatal编程技术网

Forms 使用FormEvents自定义SonataAdmin中字段的正确方法

Forms 使用FormEvents自定义SonataAdmin中字段的正确方法,forms,symfony,sonata-admin,Forms,Symfony,Sonata Admin,我有一个Sonata Admin类,其中包含一些表单字段,但我想使用FormEvents::PRE_SET_DATA事件根据绑定的数据动态添加另一个表单字段 但是,我遇到了几个问题: 1) 我能找到的将表单字段添加到管理中正确的“formGroup”的唯一方法是添加两次新字段(一次通过formMapper,一次通过表单本身)。。。这似乎是非常错误的,我无法控制它在formGroup中出现的位置 2) 添加的元素似乎不知道它有一个已连接的管理员(可能是因为它是使用Form::add()添加的)。这

我有一个Sonata Admin类,其中包含一些表单字段,但我想使用
FormEvents::PRE_SET_DATA
事件根据绑定的数据动态添加另一个表单字段

但是,我遇到了几个问题:

1) 我能找到的将表单字段添加到管理中正确的“formGroup”的唯一方法是添加两次新字段(一次通过formMapper,一次通过表单本身)。。。这似乎是非常错误的,我无法控制它在formGroup中出现的位置

2) 添加的元素似乎不知道它有一个已连接的管理员(可能是因为它是使用
Form::add()
添加的)。这意味着,除其他外,它的呈现方式与表单中的其他字段不同,因为如果未定义sonata_admin、未启用sonata_admin或未启用sonata_admin.field_description%}表单管理字段.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()
    ;
}
目的是在
MyFormGroup
中的
fieldOne
fieldTwo
之间添加新的
foo
字段


编辑:以下是我根据卡西亚诺的回答得出的结论

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()
    ;
}

没有时间在这里给出一个长的答案,我将粘贴一段代码,我不知道是否完全适合您的情况,在我的it中,它是多依赖选择(国家、州、城市、邻居)的一部分


这似乎成功了-谢谢!将更新上述问题,以显示这些地区未来旅行者使用的代码。你好@Cassiano,@Caponica,我有问题。当添加一个$form->add($ff->createName('foto.messages')时,我发现了这样一个错误:
名称“foto.messages”包含非法字符。名称应该以字母、数字或下划线开头,并且只包含字母、数字、数字、下划线(“\”)、连字符(“-”)和冒号(:”).
SOrry@webyseo无法仅用此描述帮助您,也许您可以在Stackoverflow上打开一个新问题,以便我们有更多详细信息回答您。
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);
}