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:
noteempty).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
fileandnotepresent), the item is correctly created and persists as expected.When I debug in the LiveAction
savemethod:Request($request->files->all()).$this->form()->getData()showsmyEntity => [ 'collection' => [ 0 ] ]. So the new collection item is empty / not formed.$this->formValuesshows the new item as empty strings for file and note:My conclusion: LiveComponent formValues do not contain UploadedFile instances; files arrive via the HTTP
Requestand are not automatically merged into$this->formValuesused bysubmitForm().What I tried
Before calling
$this->submitForm()I manually merged theRequestfiles 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->formValuesand the file field becomesnullagain.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
LiveCollectionTypewith Symfony UX LiveComponent?$request->filesinto$this->formValuesis 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