Fileupload: How to show a default image that the user can override with their own image
Basically I want to show a default image in the file upload form that the users can choose to replace by uploading their own image. Couldn't find an easy way to do this in the docs (but I think I am probably missing something) Thanks.
3 Replies
actually did a search on discord and found a solution haven't tired it yet:
Forms\Components\FileUpload::make('company_logo')
->label('Company Logo')
->image()
->required()
->rules([
'image',
'mimes:jpeg,png,jpg',
'max:2048',
'dimensions:max_width=300,max_height=200',
])
->formatStateUsing(fn ($record) => $record?->company_logo ?? asset('/images/company_logo.png'))
->disk('company_logo') // from filesystems.php
->maxFiles(1)
hmm oesn't seem to work
gives me an error
okay i have resolved it my use case was a bit different as I was trying to have an action (button) set the fileupload form input i was just missing the live() modifier and just set the
Forms\Components\FileUpload::make('image')
->label('Image (Optional)')
->image()
->live()
then i was able to use in my action button
$livewire->form->fill($formState);you can just override the default avatar provider (the one who generates an image from you name first letters)
see this:
https://filamentphp.com/docs/3.x/panels/users#using-a-different-avatar-provider
Thanks also i think using $set might make more sense than the $livewire form fill for some use cases