Skip to content

Commit 1c85007

Browse files
committed
minor #18722 [Form] Fix File Upload Example (nacholibre)
This PR was submitted for the 5.4 branch but it was merged into the 6.4 branch instead. Discussion ---------- [Form] Fix File Upload Example Currently the example suggests to execute this code when doing edit ```php $product->setBrochureFilename( new File($this->getParameter('brochures_directory').'/'.$product->getBrochureFilename()) ); ``` this is wrong in couple of ways First the `setBrochureFilename` method expects string and not a `File` instance. No error is thrown because the `File` instance can be casted to `string` and will return the full path, which is not what's needed. In the database field you need to store the filename only, and not the full path. Second the form expects `File` instance and not the entity. This is why the form data needs to be changed and not the entity. In the 4.4 branch it's the same. Commits ------- e2fb274 Fix File Upload Example
2 parents 22a8d45 + e2fb274 commit 1c85007

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

controller/upload_file.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,13 @@ You can use the following code to link to the PDF brochure of a product:
206206
use Symfony\Component\HttpFoundation\File\File;
207207
// ...
208208

209-
$product->setBrochureFilename(
210-
new File($brochuresDirectory.DIRECTORY_SEPARATOR.$product->getBrochureFilename())
211-
);
209+
if (!$form->isSubmitted() && $product->getBrochureFilename()) {
210+
$form->setData(['brochure' => new File($this->getParameter('brochures_directory').DIRECTORY_SEPARATOR.$product->getBrochureFilename())]);
211+
}
212+
213+
if ($form->isSubmitted() && $form->isValid()) {
214+
// ...
215+
}
212216

213217
Creating an Uploader Service
214218
----------------------------

0 commit comments

Comments
 (0)