Custom FileUpload state validation after upload

I am working a a project where I need to get the TempUploadedFile from FileUpload and extract information from the image (using external API to read information) and make sure the document code is valid. If not I want to show user an error message and fail the upload (red color on FileUpload will be good indicator). This FileUpload field is located in a Form inside a FilamentPage. I have tried using afterStateUpdated(fn(FileUpload $component, Set $set) => $this->extractInformation($component, $set)) to call a function for all the logic and within it I tried using _uploadErrored as well as throwing ValidationException directly but neither of them worked. How can I achieve this? Than you for your help.
2 Replies
Hagi
Hagi2mo ago
can I see the code?
Zamion101
Zamion101OP2mo ago
private function fillInformationFromMRZ(string $type, FileUpload $component, Get $get, Set $set): void
{
try {
$tempUploadedFiles = $component->getState();
$client = MRZReaderClient::make();
$result = $client->extractFromUploadedFile(array_shift($tempUploadedFiles));
Log::channel('stack')->debug($result);

if ($result['status'] !== 'SUCCESS') {
return;
}

if ($type === 'identity_document') {
// TODO: Add Validation in some magical way
if (!Str::startsWith($result['document_code'], 'I')) {
// I want to add some kind of validation after the MRZ has been extracted and the document code is what we expected.
return;
}
$this->fillIdentityFields($result, $get, $set);
}
} catch (\Throwable $e) {
Log::error($e);
}
}

// This here is inside a Wizard / Step
FileUpload::make('identity_card_back')
->label('Identity Card (back)')
->visibility('private')
->imageEditor()
->panelLayout('integrated')
->acceptedFileTypes(['image/png', 'image/jpeg', 'image/webp', 'image/jpg'])
->live(debounce: 500)
->required()
->afterStateUpdated(fn(FileUpload $component, Get $get, Set $set) => $this->fillInformationFromMRZ('identity_document', $component, $get, $set))
->disk('storage'),
private function fillInformationFromMRZ(string $type, FileUpload $component, Get $get, Set $set): void
{
try {
$tempUploadedFiles = $component->getState();
$client = MRZReaderClient::make();
$result = $client->extractFromUploadedFile(array_shift($tempUploadedFiles));
Log::channel('stack')->debug($result);

if ($result['status'] !== 'SUCCESS') {
return;
}

if ($type === 'identity_document') {
// TODO: Add Validation in some magical way
if (!Str::startsWith($result['document_code'], 'I')) {
// I want to add some kind of validation after the MRZ has been extracted and the document code is what we expected.
return;
}
$this->fillIdentityFields($result, $get, $set);
}
} catch (\Throwable $e) {
Log::error($e);
}
}

// This here is inside a Wizard / Step
FileUpload::make('identity_card_back')
->label('Identity Card (back)')
->visibility('private')
->imageEditor()
->panelLayout('integrated')
->acceptedFileTypes(['image/png', 'image/jpeg', 'image/webp', 'image/jpg'])
->live(debounce: 500)
->required()
->afterStateUpdated(fn(FileUpload $component, Get $get, Set $set) => $this->fillInformationFromMRZ('identity_document', $component, $get, $set))
->disk('storage'),

Did you find this page helpful?