Live Collection Type (Embedded CollectionType Form) & VICH Upload Files? #3126
Unanswered
woweya
asked this question in
Questions & Answers
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello — I'm working with Symfony 7.2, PHP 8.2, Symfony UX LiveComponent and VichUploader, and I have a reproducible problem when saving collection items that contain only a file field.
Environment
What I built
I have a form that renders a
LiveCollectionType
. Each collection item form contains two fields:file
(VichFileType / input file)note
(text)In the browser I can:
note
empty).Observed behavior
When I save a newly added collection item that contains only a file (no note), the collection item is not created/persisted.
When I add a note together with the file (i.e. both
file
andnote
present), the item is correctly created and persists as expected.When I debug in the LiveAction
save
method:Request
($request->files->all()
).$this->form()->getData()
showsmyEntity => [ 'collection' => [ 0 ] ]
. So the new collection item is empty / not formed.$this->formValues
shows the new item as empty strings for file and note:My conclusion: LiveComponent formValues do not contain UploadedFile instances; files arrive via the HTTP
Request
and are not automatically merged into$this->formValues
used bysubmitForm()
.What I tried
Before calling
$this->submitForm()
I manually merged theRequest
files into$this->formValues
, e.g.:But at submit I get a validation error (even though there are no constraints on the file type). Digging deeper, it appears
$this->submitForm()
or the LiveComponent internals strip out or overwrite the modified values I inserted into$this->formValues
and the file field becomesnull
again.Workaround I considered
Count incoming files vs items in
$this->formValues
. If there are more form values than uploaded files, assume some items were added without documents and ignore those items. If counts match, process and persist. This works but feels brittle and hacky.What I want to know
LiveCollectionType
with Symfony UX LiveComponent?$request->files
into$this->formValues
is the right approach, what is the correct way to do it so LiveComponent /submitForm()
will accept the UploadedFile instances and not overwrite/remove them?Code snippets (for clarity)
FormType (simplified):
Entry Type (simplified):
If someone has solved this properly, know that you would be extremely helpful.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions