Skip to content

Commit

Permalink
Few fixes after testing the code
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed May 21, 2016
1 parent 0739ad5 commit f6068c9
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cookbook/controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Finally, you need to update the code of the controller that handles the form::
$form = $this->createForm(new ProductType(), $product);
$form->handleRequest($request);

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

// src/AppBundle/FileUploader.php
namespace AppBundle\FileUploader;
namespace AppBundle;

use Symfony\Component\HttpFoundation\File\UploadedFile;

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

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use AppBundle\Entity\Product;
use AppBundle\FileUploader;

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

public function preUpdate(LifecycleEventArs $args)
public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();

Expand Down Expand Up @@ -364,6 +366,7 @@ Now, register this class as a Doctrine listener:
arguments: ['@app.brochure_uploader']
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
.. code-block:: xml
Expand All @@ -382,6 +385,7 @@ Now, register this class as a Doctrine listener:
<argument type="service" id="app.brochure_uploader"/>
<tag name="doctrine.event_listener" event="prePersist"/>
<tag name="doctrine.event_listener" event="preUpdate"/>
</service>
</container>
Expand All @@ -398,6 +402,9 @@ Now, register this class as a Doctrine listener:
$definition->addTag('doctrine.event_listener', array(
'event' => 'prePersist',
));
$definition->addTag('doctrine.event_listener', array(
'event' => 'preUpdate',
));
$container->setDefinition('app.doctrine_brochure_listener', $definition);
This listeners is now automatically executed when persisting a new Product
Expand Down

0 comments on commit f6068c9

Please sign in to comment.