@@ -60,6 +60,7 @@ property::
60
60
use Symfony\Component\Form\Extension\Core\Type\FileType;
61
61
use Symfony\Component\Form\FormBuilderInterface;
62
62
use Symfony\Component\OptionsResolver\OptionsResolver;
63
+ use Symfony\Component\Validator\Constraints\File;
63
64
64
65
class ProductType extends AbstractType
65
66
{
@@ -69,7 +70,26 @@ property::
69
70
// ...
70
71
->add('brochure', FileType::class, [
71
72
'label' => 'Brochure (PDF file)',
73
+
74
+ // unmapped means that this field is not associated to any entity property
72
75
'mapped' => false,
76
+
77
+ // make it optional so you don't have to re-upload the PDF file
78
+ // everytime you edit the Product details
79
+ 'required' => false,
80
+
81
+ // unmapped fields can't define their validation using annotations
82
+ // in the associated entity, so you can use the PHP constraint classes
83
+ 'constraints' => [
84
+ new File([
85
+ 'maxSize' => '1024k',
86
+ 'mimeTypes' => [
87
+ 'application/pdf',
88
+ 'application/x-pdf',
89
+ ],
90
+ 'mimeTypesMessage' => 'Please upload a valid PDF document',
91
+ ])
92
+ ],
73
93
])
74
94
// ...
75
95
;
@@ -125,24 +145,29 @@ Finally, you need to update the code of the controller that handles the form::
125
145
if ($form->isSubmitted() && $form->isValid()) {
126
146
/** @var UploadedFile $brochureFile */
127
147
$brochureFile = $form['brochure']->getData();
128
- $originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
129
- // this is needed to safely include the file name as part of the URL
130
- $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
131
- $newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
132
-
133
- // Move the file to the directory where brochures are stored
134
- try {
135
- $brochureFile->move(
136
- $this->getParameter('brochures_directory'),
137
- $newFilename
138
- );
139
- } catch (FileException $e) {
140
- // ... handle exception if something happens during file upload
141
- }
142
148
143
- // updates the 'brochure' property to store the PDF file name
144
- // instead of its contents
145
- $product->setBrochureFilename($newFilename);
149
+ // this condition is required because the 'brochure' field is not required
150
+ // so the PDF file must be processed only when a file is uploaded
151
+ if ($brochureFile) {
152
+ $originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
153
+ // this is needed to safely include the file name as part of the URL
154
+ $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
155
+ $newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
156
+
157
+ // Move the file to the directory where brochures are stored
158
+ try {
159
+ $brochureFile->move(
160
+ $this->getParameter('brochures_directory'),
161
+ $newFilename
162
+ );
163
+ } catch (FileException $e) {
164
+ // ... handle exception if something happens during file upload
165
+ }
166
+
167
+ // updates the 'brochure' property to store the PDF file name
168
+ // instead of its contents
169
+ $product->setBrochureFilename($newFilename);
170
+ }
146
171
147
172
// ... persist the $product variable or any other work
148
173
@@ -296,9 +321,10 @@ Now you're ready to use this service in the controller::
296
321
if ($form->isSubmitted() && $form->isValid()) {
297
322
/** @var UploadedFile $brochureFile */
298
323
$brochureFile = $form['brochure']->getData();
299
- $brochureFileName = $fileUploader->upload($brochureFile);
300
-
301
- $product->setBrochureFilename($brochureFileName);
324
+ if ($brochureFile) {
325
+ $brochureFileName = $fileUploader->upload($brochureFile);
326
+ $product->setBrochureFilename($brochureFileName);
327
+ }
302
328
303
329
// ...
304
330
}
0 commit comments