Skip to content

Commit 8e7591a

Browse files
committed
Misc. improvements
1 parent ae2576f commit 8e7591a

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

controller/upload_file.rst

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ property::
6060
use Symfony\Component\Form\Extension\Core\Type\FileType;
6161
use Symfony\Component\Form\FormBuilderInterface;
6262
use Symfony\Component\OptionsResolver\OptionsResolver;
63+
use Symfony\Component\Validator\Constraints\File;
6364

6465
class ProductType extends AbstractType
6566
{
@@ -69,7 +70,26 @@ property::
6970
// ...
7071
->add('brochure', FileType::class, [
7172
'label' => 'Brochure (PDF file)',
73+
74+
// unmapped means that this field is not associated to any entity property
7275
'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+
],
7393
])
7494
// ...
7595
;
@@ -125,24 +145,29 @@ Finally, you need to update the code of the controller that handles the form::
125145
if ($form->isSubmitted() && $form->isValid()) {
126146
/** @var UploadedFile $brochureFile */
127147
$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-
}
142148

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+
}
146171

147172
// ... persist the $product variable or any other work
148173

@@ -296,9 +321,10 @@ Now you're ready to use this service in the controller::
296321
if ($form->isSubmitted() && $form->isValid()) {
297322
/** @var UploadedFile $brochureFile */
298323
$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+
}
302328

303329
// ...
304330
}

0 commit comments

Comments
 (0)