0

I'm trying to change the "The photo must not be greater than 1024 kilobytes." from UpdateUserProfileInformation file

class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
  public function update($user, array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
            'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
        ])->validateWithBag('updateProfileInformation');

        if (isset($input['photo'])) {
            $user->updateProfilePhoto($input['photo']);
        }

        if ($input['email'] !== $user->email &&
            $user instanceof MustVerifyEmail) {
            $this->updateVerifiedUser($user, $input);
        } else {
            $user->forceFill([
                'name' => $input['name'],
                'email' => $input['email'],
            ])->save();
        }
    }
}

I want to change that message to "The photo must not be greater than 1 MB."

2 Answers 2

2

You can set custom messages on the 3rd argument of Validator::make():

$messages = [
   'photo.max' => 'The photo must not be greater than 1 MB."',
];

Validator::make($input, [
    'name' => ['required', 'string', 'max:255'],
    'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
    'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
], $messages)
   ->validateWithBag('updateProfileInformation');
Sign up to request clarification or add additional context in comments.

Comments

1

you can use custom messages for validation like this


$messages = [
   'photo.max' => 'The photo must not be greater than 1 MB."',
];

Validator::make($input, [
    'name' => ['required', 'string', 'max:255'],
    'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
    'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
], $messages)
   ->validateWithBag('updateProfileInformation');
"
]
)->validateWithBag('updateProfileInformation');

or even more dynamic could be like this

$maxSize=1024;
$messages = [
   'photo.max' => "The photo must not be greater than {$maxSize/1024} MB.",
];

Validator::make($input, [
    'name' => ['required', 'string', 'max:255'],
    'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
    'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:'.$maxSize],
], $messages)
   ->validateWithBag('updateProfileInformation');
"
]
)->validateWithBag('updateProfileInformation');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.