Skip to content

Commit 4a7709b

Browse files
committed
Fixed all the issues spotted by Ryan
1 parent 20ae21b commit 4a7709b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

cookbook/controller/upload_file.rst

+22-6
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ in the ``Product`` entity::
4949
Note that the type of the ``brochure`` column is ``string`` instead of ``binary``
5050
or ``blob`` because it just stores the PDF file name instead of the file contents.
5151

52-
Then, add a new ``brochure`` field to the form that manages ``Product`` entities::
52+
Then, add a new ``brochure`` field to the form that manage the ``Product`` entity::
5353

5454
// src/AppBundle/Form/ProductType.php
5555
namespace AppBundle\Form;
5656

5757
use Symfony\Component\Form\AbstractType;
5858
use Symfony\Component\Form\FormBuilderInterface;
59+
use Symfony\Component\OptionsResolver\OptionsResolver;
5960

6061
class ProductType extends AbstractType
6162
{
@@ -68,7 +69,17 @@ Then, add a new ``brochure`` field to the form that manages ``Product`` entities
6869
;
6970
}
7071

71-
// ...
72+
public function configureOptions(OptionsResolver $resolver)
73+
{
74+
$resolver->setDefaults(array(
75+
'data_class' => 'AppBundle\Entity\Product',
76+
));
77+
}
78+
79+
public function getName()
80+
{
81+
return 'product';
82+
}
7283
}
7384

7485
Now, update the template that renders the form to display the new ``brochure``
@@ -91,10 +102,11 @@ Finally, you need to update the code of the controller that handles the form::
91102
// src/AppBundle/Controller/ProductController.php
92103
namespace AppBundle\ProductController;
93104

105+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
94106
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
95107
use Symfony\Component\HttpFoundation\Request;
96-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
97108
use AppBundle\Entity\Product;
109+
use AppBundle\Form\ProductType;
98110

99111
class ProductController extends Controller
100112
{
@@ -103,10 +115,13 @@ Finally, you need to update the code of the controller that handles the form::
103115
*/
104116
public function newAction(Request $request)
105117
{
106-
//...
118+
$product = new Product();
119+
$form = $this->createForm(new ProductType(), $product);
120+
$form->handleRequest($request);
107121

108122
if ($form->isValid()) {
109123
// $file stores the uploaded PDF file
124+
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
110125
$file = $product->getBrochure()
111126

112127
// Generate a unique name for the file before saving it
@@ -116,10 +131,11 @@ Finally, you need to update the code of the controller that handles the form::
116131
$brochuresDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/brochures';
117132
$file->move($brochuresDir, $fileName);
118133

119-
// Update the 'brochure' property to store the PDF file name instead of its contents
134+
// Update the 'brochure' property to store the PDF file name
135+
// instead of its contents
120136
$product->setBrochure($filename);
121137

122-
// ...
138+
// persist the $product variable or any other work...
123139

124140
return $this->redirect($this->generateUrl('app_product_list'));
125141
}

0 commit comments

Comments
 (0)