简体   繁体   English

Symfony 3.4 Ajax在Form事件中

[英]Symfony 3.4 Ajax in Form event

In my project, the form allow user select a Map in a SelectBox. 在我的项目中,该表单允许用户在SelectBox中选择一个Map。 When Map Selectbox change, the options in GroupLayer Selectbox also change depend on which Map is selected. 更改地图选择框时,GroupLayer选择框中的选项也会更改,具体取决于选择的地图。 I see exactly Symfony document for my case in: How to Dynamically Modify Forms Using Form Events Howerver, in example code: 在以下示例代码中,我可以看到关于我的案例的Symfony文档: 如何使用表单事件动态修改表单 ,示例代码:

$formModifier = function (FormInterface $form, Sport $sport = null) {
        $positions = null === $sport ? array() : $sport->getAvailablePositions();

        $form->add('position', EntityType::class, array(
            'class' => 'App\Entity\Position',
            'placeholder' => '',
            'choices' => $positions,
        ));
    };

I don't know where the getAvailablePositions() function should be and what is the returning of this function?. 我不知道getAvailablePositions()函数应该在哪里,该函数的返回值是什么? I think this function will be placed in Sport Entity. 我认为此功能将放置在Sport Entity中。 Is that right, in Sport Entity, could I query the Position Entity with Doctrine ORM queryBuilder? 是正确的,在“ Sport实体”中,我可以使用Doctrine ORM queryBuilder查询Position实体吗?

with this formModifier you only change the fields that your form have. 使用此formModifier,您仅更改表单具有的字段。 I don't know where you have the relation between Map and GroupLayer, but this relation is what you need to search. 我不知道Map和GroupLayer之间的关系在哪里,但是这种关系是您需要搜索的。 For example, if you have a OneToMany relation beetween the entities you can do: 例如,如果您在实体之间有一个OneToMany关系,则可以执行以下操作:

$map->getGroupLayers();

this was the choices for the selector. 这是选择器的选择。

In the other hand you can use a custom method from the GroupLayer repository with the map as parameter or a service that search for the related GroupLayers from a map, it's up to you and your architecture. 另一方面,您可以使用GroupLayer存储库中的自定义方法(将地图作为参数)或从地图中搜索相关GroupLayers的服务,这取决于您和您的体系结构。

Edit #1 编辑#1

With your new info i guess that your code seem near like this: 使用您的新信息,我想您的代码看起来像这样:

$formModifier = function (FormInterface $form, Map $map = null) {
    $groupLayers = null === $map ? array() : $map->getGroupLayers();

    $form->add('position', EntityType::class, array(
        'class' => 'App\Entity\GroupLayer',
        'placeholder' => '',
        'choices' => $groupLayers,
    ));
};

$builder->addEventListener(
    FormEvents::PRE_SET_DATA,
    function (FormEvent $event) use ($formModifier) {
        // this would be your entity, i.e. SportMeetup
        $data = $event->getData();

        $formModifier($event->getForm(), $data->getMap());
    }
);

$builder->get('sport')->addEventListener(
    FormEvents::POST_SUBMIT,
    function (FormEvent $event) use ($formModifier) {
        // It's important here to fetch $event->getForm()->getData(), as
        // $event->getData() will get you the client data (that is, the ID)
        $map = $event->getForm()->getData();

        // since we've added the listener to the child, we'll have to pass on
        // the parent to the callback functions!
        $formModifier($event->getForm()->getParent(), $map);
    }
);

I hope this can help you 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM