duandao6414 2018-03-21 08:48
浏览 43
已采纳

表格事件中的Symfony 3.4 Ajax

In my project, the form allow user select a Map in a SelectBox. When Map Selectbox change, the options in GroupLayer Selectbox also change depend on which Map is selected. I see exactly Symfony document for my case in: How to Dynamically Modify Forms Using Form Events Howerver, in example code:

$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?. I think this function will be placed in Sport Entity. Is that right, in Sport Entity, could I query the Position Entity with Doctrine ORM queryBuilder?

  • 写回答

1条回答 默认 最新

  • dongsuichi6529 2018-03-21 12:55
    关注

    with this formModifier you only change the fields that your form have. I don't know where you have the relation between Map and GroupLayer, but this relation is what you need to search. For example, if you have a OneToMany relation beetween the entities you can do:

    $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.

    Edit #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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵