Skip to content

Commit f6068c9

Browse files
committed
Few fixes after testing the code
1 parent 0739ad5 commit f6068c9

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cookbook/controller/upload_file.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Finally, you need to update the code of the controller that handles the form::
130130
$form = $this->createForm(new ProductType(), $product);
131131
$form->handleRequest($request);
132132

133-
if ($form->isValid()) {
133+
if ($form->isSubmitted() && $form->isValid()) {
134134
// $file stores the uploaded PDF file
135135
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
136136
$file = $product->getBrochure();
@@ -210,7 +210,7 @@ To avoid logic in controllers, making them big, you can extract the upload
210210
logic to a seperate service::
211211

212212
// src/AppBundle/FileUploader.php
213-
namespace AppBundle\FileUploader;
213+
namespace AppBundle;
214214

215215
use Symfony\Component\HttpFoundation\File\UploadedFile;
216216

@@ -304,7 +304,9 @@ automatically upload the file when persisting the entity::
304304
// src/AppBundle/EventListener/BrochureUploadListener.php
305305
namespace AppBundle\EventListener;
306306

307+
use Symfony\Component\HttpFoundation\File\UploadedFile;
307308
use Doctrine\ORM\Event\LifecycleEventArgs;
309+
use Doctrine\ORM\Event\PreUpdateEventArgs;
308310
use AppBundle\Entity\Product;
309311
use AppBundle\FileUploader;
310312

@@ -324,7 +326,7 @@ automatically upload the file when persisting the entity::
324326
$this->uploadFile($entity);
325327
}
326328

327-
public function preUpdate(LifecycleEventArs $args)
329+
public function preUpdate(PreUpdateEventArgs $args)
328330
{
329331
$entity = $args->getEntity();
330332

@@ -364,6 +366,7 @@ Now, register this class as a Doctrine listener:
364366
arguments: ['@app.brochure_uploader']
365367
tags:
366368
- { name: doctrine.event_listener, event: prePersist }
369+
- { name: doctrine.event_listener, event: preUpdate }
367370
368371
.. code-block:: xml
369372
@@ -382,6 +385,7 @@ Now, register this class as a Doctrine listener:
382385
<argument type="service" id="app.brochure_uploader"/>
383386
384387
<tag name="doctrine.event_listener" event="prePersist"/>
388+
<tag name="doctrine.event_listener" event="preUpdate"/>
385389
</service>
386390
</container>
387391
@@ -398,6 +402,9 @@ Now, register this class as a Doctrine listener:
398402
$definition->addTag('doctrine.event_listener', array(
399403
'event' => 'prePersist',
400404
));
405+
$definition->addTag('doctrine.event_listener', array(
406+
'event' => 'preUpdate',
407+
));
401408
$container->setDefinition('app.doctrine_brochure_listener', $definition);
402409
403410
This listeners is now automatically executed when persisting a new Product

0 commit comments

Comments
 (0)