Test Failed
Branch master (354693)
by Valery
12:29
created

AddAreaFieldSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
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 AddAreaFieldSubscriber implements EventSubscriberInterface
13
{
14
    public static function getSubscribedEvents()
15
    {
16
        return [FormEvents::POST_SET_DATA => 'onLocalitySelected'];
17
    }
18
19
    public function onLocalitySelected(FormEvent $event)
20
    {
21
        $form = $event->getForm();
22
        $data = $event->getData();
23
        $areas = $data->getArea();
24
25
        if ($areas) {
26
            $form->get('locality')->setData($areas->getLocality());
27
28
            $form->add('area', EntityType::class, [
29
                'class' => 'App\Entity\Area',
30
                'placeholder' => 'placeholder.select_area',
31
                'choice_label' => 'name',
32
                'attr' => [
33
                    'class' => 'form-control',
34
                ],
35
                'required' => false,
36
                'label' => 'label.area',
37
                'choices' => $areas->getLocality()->getAreas(),
38
            ]);
39
        }
40
    }
41
}
42