Fix bugs in Managing Files and Images code #744
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As mentioned in the issue api-platform/admin#146, the code proposed in the documention to handle file upload is not fully working.
In this PR, I removed the useless
<FunctionField>
component. Indeed, I see no interest using it.Other modifications don't require to be explained (minor fixes such as
value
instead ofvalue[0]
).Nevertheless, the following lines (picked from the doc) need to be discussed :
This part of code is responsible of adding a new request to upload the file. When you click on the creation button, it first sends the file to
/images/upload
. Then, the usual request/images
(the one for each creation entity) is executed.It could be resumed with the following graph:
It's really important to understand that there are two requests when clicking on the creation button.
However, the documentation of how to handle file upload in the API Platform back is not corresponding to the strategy used in the admin interface.
Let's see why in different points:
If we use the previous code in the admin interface with the Image class, an error will occur while doing the second request as the file attribute will be null (because the code only edits the rendering of the
contentUrl
attribute).We must remove the
@Assert\NotNull()
annotation.Now, if we try again in the admin interface to upload a file again, two instances of the Image class will be saved in the database (as there are two requests). The first one is the one which uploads the file as desired but the second one is empty. Also, the user is redirected to the page of the last image instance (the empty one, so).
We must then change the condition to:
and replace:
by:
It's only now that the whole photo upload system works! 馃帀
Do I need to do a PR to modify the documentation of this page or just add a note in the admin documentation part?
Maybe an easier solution could be proposed to handle file upload?
I got many difficulties to explain the problem in this PR in writing. Don't hesitate to ask me questions if I haven't been clear on some points.