Related content

There are rules in Laravel for validating specific types of attributes. File or attachment validation is one of them. And the cool thing is, there is an amazing validation rule in laravel for validating attachments or files.

In this tutorial, I will show you how to validate a file in Laravel 10 application using the rules File type class. We can normally validate file in laravel using our own custom validation rule. We can validate max size, min size, file types or many things using laravel file upload validation rule class.

Let's see the laravel 10 file upload validation preview image:

laravel-10-file-validation-exampleNow see the example code of laravel file upload validation max size:



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rules\File;
use Illuminate\Support\Facades\Validator;

class TutorialController extends Controller
    public function index()
        return view('welcome');

    public function store(Request $request)
        Validator::validate($request->all(), [
            'attachment' => [
                File::types(['mp3', 'mp4'])
                    ->max(12 * 1024),


Now show the error message in your blade file like:



<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background: gray; color:#f1f7fa; font-weight:bold;">
                    Laravel 10 Attachment Validation Example - Laravelia
                 <div class="card-body">                    
                        action="{{ route('') }}" 
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label">
                            <div class="col-sm-9">
                                <input type="file" class="form-control  @error('attachment') is-invalid @enderror" name="attachment">
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label"></label>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-success btn-block text-white">Submit</button>


Reas also: Laravel 10 Regex URL Validation Example



Now we know laravel file upload validation. Hope this laravel file validation example tutorial will help you. After completing laravel file upload validation max size, your concept will be clear about laravel file upload validation example.