F
Filament4mo ago
NozmaD

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
NozmaDOP3mo 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
LeandroFerreira3mo ago
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
NozmaDOP3mo 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
LeandroFerreira3mo ago
when you save the form?
NozmaD
NozmaDOP3mo ago
No, when upload file
LeandroFerreira
LeandroFerreira3mo ago
add this
->afterStateUpdated(function (Page $livewire, FileUpload $component) {
$livewire->validateOnly($component->getStatePath());
})
->afterStateUpdated(function (Page $livewire, FileUpload $component) {
$livewire->validateOnly($component->getStatePath());
})
NozmaD
NozmaDOP3mo 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
NozmaDOP3mo ago
Like this
No description
LeandroFerreira
LeandroFerreira3mo ago
hum, not sure if you can do this..
NozmaD
NozmaDOP3mo ago
Ok, thx 🙂

Did you find this page helpful?