file upload validation - file name must start with value

Is it possible to do something like this on a file upload component? this doesn't work, but it's the idea I'm going for:
->rules([
function ($attribute, $value, $fail) {
$fileName = $value->getClientOriginalName();
$startsWith = 'required_prefix_';

if (!str_starts_with($fileName, $startsWith)) {
$fail("The file name must start with {$startsWith}.");
}
},
])
->rules([
function ($attribute, $value, $fail) {
$fileName = $value->getClientOriginalName();
$startsWith = 'required_prefix_';

if (!str_starts_with($fileName, $startsWith)) {
$fail("The file name must start with {$startsWith}.");
}
},
])
1 Reply
awcodes
awcodes3mo ago
Do you need a validation? To me, at least, it would make more sense to customize the save and append the prefix if it doesn’t exist. But I would expect your code to work, assuming you are preserving file names on the upload. They aren’t preserved by default since laravel defaults to a unique ‘uuid’ for the file name.
Want results from more Discord servers?
Add your server