Abdessamad Ounaceur
Abdessamad Ounaceur

Reputation: 11

Symfony 4.4 - Easy Admin 3: Filtering AssociationField based on another Field value

i am new to easyAdmin v3, and i don't know how to filter AssociationField based on another one in the same form. this is the two fields:

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

i try to add this

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

any idea please, thanks in advance.

Upvotes: 0

Views: 1948

Answers (1)

Abdessamad Ounaceur
Abdessamad Ounaceur

Reputation: 11

I found the solution by myself using normal formbuilder of symfony instead of AssociationField: by i still search if anyone can do it using dynamic filtering of 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;
    }

and on the twig file : 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>

Upvotes: 0

Related Questions