Home > database >  How to display a custom message in laravel fortify
How to display a custom message in laravel fortify

Time:02-22

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."

CodePudding user response:

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');

CodePudding user response:

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');
  • Related