Cancell file upload

Hi. How can I cancel the file upload to FileUpload? The verification condition is as follows: if the name of the uploaded file is not found in the database, then cancel its download.
protected function getFormSchema(): array
{
$currentTenantId = Filament::getTenant()->id;
$UserMatchList = \App\Models\Goods\Case\UserMatchList::query()
->where('cabinet_id', $currentTenantId)
->get()
->map(function ($item) {
return $item->vendor_code;
})
->toArray();

$component = Components\FileUpload::make('attachments')
->hiddenLabel()
->directory("cases/{$currentTenantId}")
->acceptedFileTypes(['image/jpeg'])
->multiple()
->preserveFilenames()
->fetchFileInformation(false)
->moveFiles(true)
->panelLayout('grid')
->afterStateUpdated(function (Set $set, Components\FileUpload $component, $state, $livewire) use ($UserMatchList) {

/** @var \Livewire\Features\SupportFileUploads\TemporaryUploadedFile $file */
foreach ($state as $file) {
$path = $file->getRealPath();
$fileName = pathinfo($path, PATHINFO_FILENAME);

if (!in_array($fileName, $UserMatchList)) {
Notification::make()
->title('Filename Error')
->danger()
->send();
$file->delete();// Dont work
return;
}
}
});


return [
Components\Fieldset::make('cases')
->schema([$component])

];
}
protected function getFormSchema(): array
{
$currentTenantId = Filament::getTenant()->id;
$UserMatchList = \App\Models\Goods\Case\UserMatchList::query()
->where('cabinet_id', $currentTenantId)
->get()
->map(function ($item) {
return $item->vendor_code;
})
->toArray();

$component = Components\FileUpload::make('attachments')
->hiddenLabel()
->directory("cases/{$currentTenantId}")
->acceptedFileTypes(['image/jpeg'])
->multiple()
->preserveFilenames()
->fetchFileInformation(false)
->moveFiles(true)
->panelLayout('grid')
->afterStateUpdated(function (Set $set, Components\FileUpload $component, $state, $livewire) use ($UserMatchList) {

/** @var \Livewire\Features\SupportFileUploads\TemporaryUploadedFile $file */
foreach ($state as $file) {
$path = $file->getRealPath();
$fileName = pathinfo($path, PATHINFO_FILENAME);

if (!in_array($fileName, $UserMatchList)) {
Notification::make()
->title('Filename Error')
->danger()
->send();
$file->delete();// Dont work
return;
}
}
});


return [
Components\Fieldset::make('cases')
->schema([$component])

];
}
Solution:
I think you can use a custom rule, because the file is moved when you submit the form ```php FileUpload::make('attachments') ......
Jump to solution
10 Replies
NozmaD
NozmaDOP8h ago
Can anyone tell me? Or maybe through the
rule()
rule()
, you can somehow check the file name from the database to make a check? Can anyone help?
Solution
LeandroFerreira
I think you can use a custom rule, because the file is moved when you submit the form
FileUpload::make('attachments')
...
->rules([
fn ($state): Closure => function (string $attribute, $value, Closure $fail) use($state) {

//$state is the fileupload state
if(...) {
return $fail('invalid.');
}
},
])
FileUpload::make('attachments')
...
->rules([
fn ($state): Closure => function (string $attribute, $value, Closure $fail) use($state) {

//$state is the fileupload state
if(...) {
return $fail('invalid.');
}
},
])
NozmaD
NozmaDOP7h ago
I tried to return the error anyway. Does not work 😦
->rules([
fn($state): \Closure => function (string $attribute, $value, \Closure $fail) use ($state) {

return $fail('invalid.');

},
])
->rules([
fn($state): \Closure => function (string $attribute, $value, \Closure $fail) use ($state) {

return $fail('invalid.');

},
])
LeandroFerreira
when you save the form?
NozmaD
NozmaDOP7h ago
No, when upload file
LeandroFerreira
add this
->afterStateUpdated(function (Page $livewire, FileUpload $component) {
$livewire->validateOnly($component->getStatePath());
})
->afterStateUpdated(function (Page $livewire, FileUpload $component) {
$livewire->validateOnly($component->getStatePath());
})
NozmaD
NozmaDOP7h ago
Yes, it works. Is it possible to do something so that this picture does not load? and there was an error as with the maximum file size
NozmaD
NozmaDOP7h ago
Like this
No description
LeandroFerreira
hum, not sure if you can do this..
NozmaD
NozmaDOP6h ago
Ok, thx 🙂

Did you find this page helpful?