-1

我是easyAdmin v3 的新手,我不知道如何根据相同形式的另一个过滤AssociationField。这是两个字段:

            AssociationField::new('brand', 'brand')->hideOnIndex(),
            AssociationField::new('model', 'Model'),

我尝试添加这个

 ->setFormTypeOptions([
        "choices" => $brande = $this->getDoctrine()->getRepository(Brande::class)->findOneBy([
            'id' => i cant get the value of brand here
        ])->getModel()->toArray()
    ]),

请有任何想法,在此先感谢。

4

1 回答 1

0

我自己使用symfony的普通表单生成器而不是AssociationField找到了解决方案:我仍然搜索是否有人可以使用AssociationField的动态过滤来做到这一点;

public function createNewFormBuilder(EntityDto $entityDto, KeyValueStore $formOptions, AdminContext $context): FormBuilderInterface {
        $formBuilder = parent::createNewFormBuilder($entityDto, $formOptions, $context);
        $formBuilder->get('brande')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) {
                $brande = $event->getForm()->getData();
                $form = $event->getForm();
                $form->getParent()->add('model', EntityType::class, [
                    'class' => Model::class,
                    'placeholder' => '',
                    'choices' => $brande ? $brande->getModel() : [],
                ]);
            }
        );
        $formBuilder->addEventListener(
            FormEvents::POST_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();
                $form->add('model', EntityType::class, [
                    'class' => Model::class,
                    'placeholder' => '',
                    'choices' => [],
                ]);
            }
        );
        return $formBuilder;
    }

并在树枝文件上:new.html.twig

{% block configured_javascripts %}
    {{ parent() }}
    {{ include('@EasyAdmin/includes/_js_assets.html.twig', { assets: new_form.vars.ea_crud_form.assets.jsAssets }, with_context = false) }}
    {{ include('@EasyAdmin/includes/_encore_script_tags.html.twig', { assets: new_form.vars.ea_crud_form.assets.webpackEncoreAssets }, with_context = false) }}
    <script type="text/javascript" src="{{ asset('js/jquery-1.12.0.min.js') }}"></script>
    <script type="text/javascript">
    
        $( document ).ready(function() {
            let deviceBrande = $('#Device_brande');
            deviceBrande.change(function() {
                // ... retrieve the corresponding form.
                var form = $(this).closest('form');
                // Simulate form data, but only include the selected sport value.
                var data = {};
                data[deviceBrande.attr('name')] = deviceBrande.val();
                // Submit data via AJAX to the form's action path.
                $.ajax({
                    url : form.attr('action'),
                    type: form.attr('method'),
                    data : data,
                    complete: function(html) {
                    // Replace current position field ...
                    $('#Device_model').replaceWith(
                        // ... with the returned one from the AJAX response.
                        $(html.responseText).find('#Device_model')
                    );
                    // Position field now displays the appropriate positions.
                    }
                });
                
            });

        });
    </script>
于 2022-01-24T22:01:49.343 回答