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:

app/Http/Controllers/TutorialController.php

<?php

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' => [
                'required',
                File::types(['mp3', 'mp4'])
                    ->min(1024)
                    ->max(12 * 1024),
            ]
        ]);
    }
}

 

Now show the error message in your blade file like:

resources/views/welcome.blade.php

@extends('layouts.app')

@section('content')
<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>
                 <div class="card-body">                    
                    <form 
                        action="{{ route('user.store') }}" 
                        method="post" 
                        enctype="multipart/form-data"
                    >
                    @csrf
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label">
                                Attachment
                            </label>
                            <div class="col-sm-9">
                                <input type="file" class="form-control  @error('attachment') is-invalid @enderror" name="attachment">
                                @error('attachment')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <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>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Reas also: Laravel 10 Regex URL Validation Example

 

Conclusion

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.