-2

Diocese:

-----------------
|id|diocese_name|
-----------------

|1 |   DJ       | 
|2 |   Kpg      |

OneToMany

Parish:
---------------------------
|id|diocese_id|parish_name|
---------------------------
|1 |    1     |St. Jude   |
|2 |    1     |St. Mark   |
|3 |    2     |St. Peter  |
|4 |    2     |St. thomas | 

I want to make a drop down dynamic: when the user clicks on Diocese name, It should Display the related Parish Name.

I tried with Form Event, but I couldn't follow the documentation. Here is the Code.

        $builder
        ->add('diocese_name', EntityType::class,[

            'class' => 'App\Entity\Diocese',
            'choice_label' => 'diocese_name'
        ])
    ;

    $builder
        ->addEventListener(

            FormEvents::PRE_SET_DATA,
            function(FormEvent $event){
                $form = $event->getForm();
                $data = $event->getData();

                $parish = $data->getParish();

                dump($parish);



            }
        );
Pranan Subba
  • 186
  • 3
  • 10

1 Answers1

0

You don't need to use any event listener. The best way is to use "Custom Form" of Symfony.

Look at the documentation :

https://symfony.com/doc/current/form/create_custom_field_type.html https://symfony.com/doc/current/form/form_customization.html

You have to create a "choicetype" for your diocese and custom the field(like first link above). In your twig ( by default the file: '.../fields.html.twig'), make a

{{ dump(form) }}

to see what variable you can access !

If you want to use the entity manager to make Doctrine request, you can ! You could create a custom FormType, then pass result of this request in your twig with attr or other.

Hope it will help, Best regards !

A.DREY
  • 38
  • 5