Form builder - File fields?

How are we handling files in the form builder? When save to json it won't repopulate as an array as it expects a string. Usually we cast on the model, but can't when we don't know the fields
10 Replies
Dennis Koch
Dennis Koch2y ago
Do you have an example of your issue?
toeknee
toekneeOP2y ago
Wizard\Step::make('Documents')
->statePath('documents')
->visible(fn (Closure $get) => (int) $get('employment_details.accept_or_decline'))
->columns(12)
->schema([
Placeholder::make('Please upload the following documents:')->columnSpan(12),
// Passport
FileUpload::make('document1')
->label('document1')
->disk('private')
->storeFileNamesIn('data-forms/'.$this->instance_id)
->required()
->columnSpan(6),
Wizard\Step::make('Documents')
->statePath('documents')
->visible(fn (Closure $get) => (int) $get('employment_details.accept_or_decline'))
->columns(12)
->schema([
Placeholder::make('Please upload the following documents:')->columnSpan(12),
// Passport
FileUpload::make('document1')
->label('document1')
->disk('private')
->storeFileNamesIn('data-forms/'.$this->instance_id)
->required()
->columnSpan(6),
Stored with 'data' => $this->data Then we fill with the data from the submission when reviewing / editing with $this->form->fill([$submission]); But the files cause Filament\Forms\Components\BaseFileUpload::Filament\Forms\Components{closure}(): Argument #1 ($file) must be of type string, array given because they haven't been cast properly as I understand it
Dennis Koch
Dennis Koch2y ago
Hm. No sure why the FileUpload stores as array but requires a string.
toeknee
toekneeOP2y ago
"documents": {"cv": {"c51222bc-84ed-4ddc-8e9c-cd20300ade13": {"disk": "local"}}}
"documents": {"cv": {"c51222bc-84ed-4ddc-8e9c-cd20300ade13": {"disk": "local"}}}
as an example is what is saved. But.. we should cast it and I think because we are not casting it, it's storing incorrectly. I am thinking I need to manually cast it on save 😦 Or I create a new view for file uploads which store in a file_uploads table with the needed casts.
Dennis Koch
Dennis Koch2y ago
Where's the filename though? Oh didn't know we have a ->storeFileNamesIn
toeknee
toekneeOP2y ago
Yeah, it’s ok I think I’ve got it though
Dan Harrin
Dan Harrin2y ago
@toeknee_iom storeFileNamesIn() is not the same thing it is used to store the file names in a column, not a storage location
toeknee
toekneeOP2y ago
You spotted my error as I did when Dennis mentioned it 😂
toeknee
toekneeOP2y ago
@danharrin so, I have the form which is a standard form builder form, with a statePath set on a wizard tab for documents. A document shows storage disk local and I've removed all config from the file upload field and this is what is stored in $this->data on saving. This is in a livewire component. Is there some docs that explains how best to handle the file saving on this form within the livewire component as I suspect we need to handle it custom?
toeknee
toekneeOP2y ago
IT doesn't seem to follow standard livewire file uploads, as document is array as the anove Think I've got it, missed that files can be multiple so it's an array first then a sub array. FYI resolved, ensure using $this->form->getState() when saving which will ensure the files are processed etc and then you can loop through. I was using $this->data as we push and map to it.
Want results from more Discord servers?
Add your server