getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Form\EventSubscriber;
6
7
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Form\FormEvent;
10
use Symfony\Component\Form\FormEvents;
11
12
class UpdateDistrictFieldSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @return string[]
16
     */
17
    public static function getSubscribedEvents(): array
18
    {
19
        return [FormEvents::POST_SUBMIT => 'onCityChanged'];
20
    }
21
22
    public function onCityChanged(FormEvent $event): void
23
    {
24
        $form = $event->getForm();
25
26
        if ($form->getData()) {
27
            // District of the city
28
            $districts = $form->getData()->getDistricts();
29
30
            $form->getParent()->add('district', EntityType::class, [
31
                'class' => 'App\Entity\District',
32
                'placeholder' => 'placeholder.select_district',
33
                'choice_label' => 'name',
34
                'attr' => [
35
                    'class' => 'form-control',
36
                ],
37
                'required' => false,
38
                'label' => 'label.district',
39
                'choices' => $districts,
40
            ]);
41
        }
42
    }
43
}
44