forked from kiloutyg/docauposte2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncidentType.php
executable file
·93 lines (85 loc) · 3.3 KB
/
IncidentType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace App\Form;
use App\Repository\ProductLineRepository;
use App\Entity\Incident;
use App\Entity\ProductLine;
use App\Entity\IncidentCategory;
use Psr\Log\LoggerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\StringerType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
// This class is responsible for creating the form for the incident entity and transforming the data to be used by the controller and the entit.
// It also contains the logic for the form validation and the form submission.
class IncidentType extends AbstractType
{
private $productLineRepository;
private $logger;
public function __construct(ProductLineRepository $productLineRepository, LoggerInterface $logger)
{
$this->productLineRepository = $productLineRepository;
$this->logger = $logger;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('file', FileType::class, [
'label' => 'Select a file to upload:',
'mapped' => true,
'required' => false,
])
->add('name', TextType::class, [
'label' => 'Nouveau nom du fichier d\'incident:',
'required' => false,
'empty_data' => null,
])
->add(
'productline',
EntityType::class,
[
'class' => ProductLine::class,
'choice_label' => 'name',
'label' => 'Select a productline:',
'placeholder' => 'Choisir un Produit',
'required' => true,
'multiple' => false,
]
)
->add(
'incidentCategory',
EntityType::class,
[
'class' => IncidentCategory::class,
'choice_label' => 'name',
'label' => 'Select an incident category:',
'placeholder' => 'Choisir un type d\'incident',
'required' => true,
'multiple' => false,
]
);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
// If no filename was submitted, set it to the original filename
if (empty($data['name'])) {
$data['name'] = $form->getData()->getName();
$event->setData($data);
}
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Incident::class,
]);
}
}