Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Seydina's avatar

Custom validation message in Laravel 10 (with breeze)

Hi, when I upload file to update my profile picture all validation rules (image, mimes) works fine except one : max:3072. It show me this default message in this screenshot below when I try to upload an image greater than 3MB:

<?php

namespace App\Http\Requests;

use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfileUpdateRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        return [
            'name' => ['string', 'max:255'],
            'email' => ['email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
            'picture' =>['image', 'mimes:jpg,jpeg,png,gif', 'max:3072'],
        ];
    }
}

And the ProfileController code :

<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProfileUpdateRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;
use App\Models\User;
use Illuminate\Validation\Rules\File;

class ProfileController extends Controller
{
    /**
     * Display the user's profile form.
     */
    public function edit(Request $request): View
    {
        return view('profile.edit', [
            'user' => $request->user(),
        ]);
    }

    /**
     * Update the user's profile information.
     * 
     */
    public function update(ProfileUpdateRequest $request): RedirectResponse
    {
        $request->user()->fill($request->validated());

        if ($request->user()->isDirty('email')) {
            $request->user()->email_verified_at = null;
        }
        
        $this->validate($request, [
            'picture' => 'image|mimes:png,gif|max:3072',
        ]);
      

        if ($request->hasFile('picture')) {
            // Handle updating the user's picture as you did in the 'store' method
            $path = $request->file('picture')->storePublicly('pictures');

            $request->user()->picture = $path;
        }


        $request->user()->save();

        return Redirect::route('profile.edit')->with('status', 'profile-updated');
    }
    /**
     * Delete the user's account.
     */
    public function destroy(Request $request): RedirectResponse
    {
        $request->validateWithBag('userDeletion', [
            'password' => ['required', 'current_password'],
        ]);

        $user = $request->user();

        Auth::logout();

        $user->delete();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return Redirect::to('/');
    }
}

I want to target this default message The picture failed to upload and customize it. After reading laravel docs about custom validation I publish lang directory with php artisan lang:publish. After that I found in lang/en directory a file validation.php in which I found this message : 'uploaded' => 'The :attribute failed to upload.',. How should we do so that the message The picture failed to upload become 'The picture must not be greater than 3072 kilobytes.'?

0 likes
31 replies
sanusi's avatar

@Seydina In your request file you can try adding method to handle messages

 public function messages()
    {
        return [
            'picture.max' => 'The picture must not be greater than  :max kilobytes.',
        ];
    }
Seydina's avatar

@sanusi I added this this function to ProfileUpdateRequest but should I call this function somewhere? And where? Because the issue remain

Snapey's avatar

@Seydina This messages function just needs to exist in the form request. It will be called automatically

Snapey's avatar

@Seydina You should remove this from your controller

        $this->validate($request, [
            'picture' => 'image|mimes:png,gif|max:3072',
        ]);
      

You are validating twice, and the bespoke message is being overwritten by the controller validation

sanusi's avatar

@Seydina Inside your update method in ProfileController I would suggest checking if the request is validated first

public function update(ProfileUpdateRequest $request)
{
	//Make sure the request is validated 
	$request->validated();
    $request->user()->fill($request->validated());
}

And also there is no need to set a return type

Seydina's avatar

@Snapey I remove this code from my ProfileController and keep picture validation uniquely in one place (ProfileUserRequest). The custom messages function is also moved to FormRequest but the issue is still here. The size validation message is not updated as I want. I see also 3 errors shown on FormRequest (see the screenshot below) which I don't know where they come from :

Snapey's avatar

@sanusi There are issues with your suggestion.

$request->validated() 

returns the validated data. It does not run validation. If validation fails then the controller is never called, therefore this comment is pointless.

Secondly, it is not possible to just fill the user model with validated data when that data contains a file input. The file must be saved, and then the path added to the user model - not the uploaded file object.

Snapey's avatar

@Seydina Do you get correct validation errors when you leave out the email address for instance?

Snapey's avatar

@Seydina regarding the errors. This is odd.

I would delete the vendor folder and run composer install.

Seydina's avatar

@Snapey for email address even when I update with these dummy words like abdc@blabla it works well but if I omit one part, the browser validation is running

Seydina's avatar

@Snapey As you suggest, I removed the vendor folder and run composer install but the errors are always shown

Snapey's avatar

@Seydina bear in mind that these errors are in VS code only. If they were 'real' issues then the code would crash.

If you are using VS code, its worth running the (command shift P) command "index Workspace"

Snapey's avatar

@Seydina

but if I omit one part, the browser validation is running

don't be confused and misled by browser validation. I'm asking if Laravel validation works

Seydina's avatar

@Snapey Ok I run this command even if I see errors again but as you said it's in VSCODE only

Seydina's avatar

@Snapey Yes Laravel validation works for all input. Only the picture max size message customization is not updated

sanusi's avatar

@seydina Have you checked if you're still receiving the validation error message after adding the "messages" method?

Seydina's avatar

@sanusi yes I checked but still I received The picture failed to upload

tangtang's avatar

@seydina

did you try to add array after messages() in your ProfileUpdateRequest function.

like this for example

public function messages(): array
{
    return [
        'picture.max' => 'Your custom message here . . .',
    ];
}

and dont do validate picture code in your ProfileController

Seydina's avatar

@tangtang I add this code in my ProfileUpdateRequest with array as return type and validation is moved from ProfileController to ProfileUpdateRequest but the issue is the same.

Seydina's avatar

In lang/en/validation.php folder I found a rule named 'uploaded' => 'The :attribute failed to upload.', that I customized (in validation.php) to this message below without adding a function messages anywhere :

'custom' => [
        'picture.uploaded' => 'The picture must not be greater than :max kilobytes.',
        ],

and I get this result for file greater than 3MB : The picture must not be greater than :max kilobytes. . As you can see I got the message I wanted but :max is not evaluated and If I want :max to be evaluated I must harcoded it.

I also get the same result when I use a function messages in my ProfileUpdateRequest :

public function messages() :array
   {
       return [
           'picture.uploaded' => 'The picture must not be greater than :max kilobytes.',
       ];
   }

Now I do not know if this is the right way to do it.

Seydina's avatar

@Snapey Illuminate\Support\ViewErrorBag {#305 ▼ // resources/views/components/picture-input.blade.php #bags: []

Seydina's avatar

@Snapey

Illuminate\Support\ViewErrorBag {#303 ▼ // resources/views/profile/partials/update-profile-information-form.blade.php
 #bags: array:1 [▼
   "default" => Illuminate\Support\MessageBag {#304 ▼
     #messages: array:1 [▼
       "picture" => array:1 [▼
         0 => "The picture must not be greater than :max kilobytes."
       ]
     ]
     #format: ":message"
   }
 ]
}
Snapey's avatar

@Seydina try

'picture.uploaded' => 'The picture must not be greater than :value kilobytes.'
Seydina's avatar

@Snapey I try it, the result is : The picture must not be greater than :value kilobytes.

Snapey's avatar

Starting again.

Does your image upload if it is smaller than 3MB and you remove the picture validation rule total?

Next, Does your image upload if it is smaller than 3MB and you include the picture rule but leave out the max rule?

Seydina's avatar

@Snapey

Does your image upload if it is smaller than 3MB and you remove the picture validation rule total?

Yes it upload

Next, Does your image upload if it is smaller than 3MB and you include the picture rule but leave out the max rule?

Yes it upload

Please or to participate in this conversation.