webso's avatar
Level 5

Livewire pdf validation

Livewire file uploads with photos is pretty straight-forward but does anyone know if there is any built in validation for other file types? I need to to validate pdf, docx, etc. for resume upload. Didn't see it in the docs anywhere or any examples on the web. If not I'll just use something custom like this but wondering if there is a simpler way with livewire.

 $type = $request['type'];
 $file = request()->file('file');
$supported_types = [
            "txt" => "Text File",
            "docx" => "Word Document",
            "rtf" => "Rich Text Format",
            "pdf" => "Portable Document Format"
        ];

$file_type = $file->getClientOriginalExtension();

if(!array_key_exists($file_type, $supported_types)){
            return redirect('/original-page')->with('unsupported_file_type', "The file selected in not a supported file type");
        }
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Livewire does not really change anything. Your regular validation rules are available, eg mimes

don't just rely on the file extension

webso's avatar
Level 5

Gotcha, makes sense. Thanks!

Please or to participate in this conversation.