How to change Laravel Validation message for max file size in MB instead of KB?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Change Laravel Validation Message for Max File Size in MB Instead of KB

As developers working with Laravel, we often deal with input validation, and sometimes the default messages provided by the framework don't perfectly align with user expectations. One common scenario involves file size limits, where Laravel defaults to showing sizes in kilobytes (KB), which can be cumbersome for users who are accustomed to megabytes (MB).

If you are enforcing a maximum file size using rules like file and max, the resulting error message reflects this default unit. For instance, you might see: "The :attribute may not be greater than :max kilobytes." While technically correct, displaying KB instead of the more intuitive MB can lead to confusion.

This guide will walk you through the most robust way to customize Laravel’s validation error messages to display file sizes in megabytes (MB) instead of kilobytes (KB), ensuring a better user experience while maintaining clean, idiomatic code.


Understanding the Default Behavior

When you use standard validation rules within your controller or request classes, Laravel automatically generates error messages based on the attribute name and the rule applied. For file uploads, the system often defaults to using base units like kilobytes unless explicitly overridden.

The default message structure looks something like this:

// Example of the default behavior you are trying to change
'file' => 'The :attribute may not be greater than :max kilobytes.'

To achieve a display in MB, we cannot simply change the validator itself; we must intercept the message generation process and perform the necessary unit conversion before presenting it to the user.

The Solution: Customizing Validation Messages

The most effective approach in Laravel is to leverage the messages() method on your Request object or Validator instance. This allows you to define custom strings for specific validation failures, enabling us to inject our desired formatting logic.

Since we need dynamic calculation (KB to MB conversion), we will write a closure within the message definition that calculates the size and formats it appropriately.

Step-by-Step Implementation

Let's assume you are handling an upload request where you validate the file size. We will define a custom message specifically for our file size constraint.

1. Define the Conversion Logic:
We need a helper function or logic to handle the conversion from bytes (which is what file sizes are measured in) to megabytes, and then format it nicely.

2. Implement Custom Messages:
In your Request class (or wherever you handle validation), use the messages() method to override the default error text for the specific rule.

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class FileUploadRequest extends Request
{
    /**
     * Get the validation messages for the request.
     *
     * @return array
     */
    public function messages()
    {
        return [
            // Custom message for file size validation
            'file_size_error' => [
                'required' => 'A file must be uploaded.',
                'max' => function ($attribute, $value) {
                    // $value here is the max size in KB (or bytes, depending on how you set up the rule).
                    // For simplicity, let's assume we are checking against a maximum size.
                    $maxKb = $value; // Assuming validation handles input as KB for this example context

                    // Convert KB to MB and format it for display
                    $maxMb = $maxKb / 1024;

                    return "The file size cannot exceed {$maxMb} megabytes.";
                },
            ],
        ];
    }
}

3. Applying the Custom Rule (Controller Logic):
In your controller, you would then use this custom message name when applying the rule:

// In your controller method
$request->validate([
    'file' => [
        'required',
        'file',
        'max:10240', // Example max size in KB (10MB = 10240KB)
    ],
]);

By defining a custom message that accepts the attribute and value, we gain access to the validation context. Inside the closure, we perform the necessary mathematical conversion ($\text{MB} = \text{KB} / 1024$) and inject this calculated value into the error string. This gives you precise control over exactly what the end-user sees.

Conclusion

Customizing default Laravel validation messages is a powerful technique that bridges the gap between framework functionality and user experience. While Laravel provides excellent scaffolding, leveraging custom message definitions allows senior developers to impose specific domain logic—like unit conversion—directly into the error feedback loop. By using methods like messages() and closures for dynamic calculation, you ensure that your application not only validates data correctly but also communicates those constraints in the most human-readable format possible, which is a core principle when building robust systems on platforms like Laravel.