F
Filament2y ago
Lea

acceptedFileTypes(['text/csv']) doesn't validate CSV with semicolons

I'm trying to use FileUpload, but the csv file only works if the delimeter is , (commas) When I upload one with ; (semicolons) it doesn't validate I kind of fix it adding text/plain, but I need to validate at least that the extensions ends with .csv, xls or xlsx Anyone knows how? ->acceptedFileTypes( [ 'csv', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel', 'application/vnd.oasis.opendocument.spreadsheet', 'text/plain' ] )
2 Replies
Dan Harrin
Dan Harrin2y ago
i havent found a good way to do this either i think its a livewire limitation
toeknee
toeknee2y ago
What about a custom rule, rough example below.
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class SemicolonSeparatedCsvRule implements Rule
{
// ...

public function passes($attribute, $value)
{
// Make sure the file is a valid CSV file
if (!$value->isValid() || !in_array($value->getMimeType(), [
'text/csv',
'text/plain',
'application/csv',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
'application/vnd.oasis.opendocument.spreadsheet',
])) {
return false;
}

// Read the contents of the file
$contents = file_get_contents($value->getRealPath());

// Check if the contents contain semicolon as a delimiter
if (strpos($contents, ';') === false) {
return false;
}

return true;
}

}
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class SemicolonSeparatedCsvRule implements Rule
{
// ...

public function passes($attribute, $value)
{
// Make sure the file is a valid CSV file
if (!$value->isValid() || !in_array($value->getMimeType(), [
'text/csv',
'text/plain',
'application/csv',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
'application/vnd.oasis.opendocument.spreadsheet',
])) {
return false;
}

// Read the contents of the file
$contents = file_get_contents($value->getRealPath());

// Check if the contents contain semicolon as a delimiter
if (strpos($contents, ';') === false) {
return false;
}

return true;
}

}
use App\Rules\SemicolonSeparatedCsvRule;

$validatedData = $this->validate([
'file' => [
'required',
'file',
new SemicolonSeparatedCsvRule(),
],
]);
use App\Rules\SemicolonSeparatedCsvRule;

$validatedData = $this->validate([
'file' => [
'required',
'file',
new SemicolonSeparatedCsvRule(),
],
]);
Want results from more Discord servers?
Add your server