Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error message not working #48866

Closed
begueradj opened this issue Oct 31, 2023 · 14 comments
Closed

Custom error message not working #48866

begueradj opened this issue Oct 31, 2023 · 14 comments

Comments

@begueradj
Copy link

begueradj commented Oct 31, 2023

Laravel Version

10.x

PHP Version

8.1

Database Driver & Version

MySql 8, Ubuntu 20.04

Description

I am using Laravel Breeze and I want to display a custom error message related to the password input during user registration.
But it is always the default error message that gets displayed.

Steps To Reproduce

In app/Http/Controllers/Auth/RegisteredUserController.php", I want to set a custom error message (please read the 2 comments):

public function store(Request $request): RedirectResponse
    {
        $messages = [
            // This custom message gets displayed properly
            'name.required' => 'We need your name',
            // This custom error message is not displayed.  The default one gets displayed, instead:
            'password.letters' => 'Your keyboard should have letters.', 
        ];
        $rules = [
            'name' => ['required', 'string'],
            'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
            'password' => ['required', 'confirmed', Password::min(3)->letters()]
        ];
        $request->validate($rules, $messages);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        event(new Registered($user));

        Auth::login($user);

        return redirect(RouteServiceProvider::HOME);
    }

From this source file and the documentation, what I did should work.

@driesvints
Copy link
Member

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

@begueradj
Copy link
Author

@driesvints Thank you for the feedback but I believe this is a bug.

@driesvints
Copy link
Member

@begueradj try setting 'validation.password.letters' instead or else try a json translation file of The :attribute must contain at least one letter.

@begueradj
Copy link
Author

Thank you.
I tried that too because I found it in the source code itself. But it did not work.
What I did is to run php artisan lang:publishand modified that message within validation.php file.
But I do not like the solution I did.

I am looking for something similar to what the documentation says about non built-in methods.
The documentation does not cover anything about this (built-in methods). If they are supposed to work in the classic way, then this is clearly a bug.

@driesvints
Copy link
Member

@begueradj can you try this?

$messages = [
    'password' => ['letters' => 'Your keyboard should have letters.'], 
];

@begueradj
Copy link
Author

I just tried what you suggested: no message is displayed in this case.

@driesvints driesvints reopened this Nov 2, 2023
@driesvints
Copy link
Member

I don't know what's up unfortunately.. appreciating any help with this.

Copy link

github-actions bot commented Nov 2, 2023

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@smkbd
Copy link

smkbd commented Nov 6, 2023

@begueradj
Password::min(3)->letters() what's the namespace for it? Is it a custom Validation Rule? If so, you can add the validation message in that particular Rule class. If not, you can create a custom validation rule to achieve what you want.
https://laravel.com/docs/10.x/validation

It seems 'password.letters' is not present in the validation error message bag. That's why you are not getting the value for it.

@rcknr
Copy link
Contributor

rcknr commented Nov 6, 2023

@begueradj It appears that Illuminate\Validation\Rules\Password doesn't support custom messages for some of its validations. As you can see in the source file you've linked some rules are added after Validator is made and their messages are retrieved solely through translations. So only thing you could do is use another validator. For example:

$messages = [
    'password' => ['regex' => 'Must contain letters']
];
$request->validate([
    'password' => ['required', 'confirmed', 'regex:/\pL/u'],
], $messages);

@rcknr
Copy link
Contributor

rcknr commented Nov 6, 2023

One can argue that the Password validation rule should use addFailure() method instead of errors()->add() to use existing message retrieval instead of bypassing it.

@begueradj
Copy link
Author

Thank you @rcknr What you said should be added to the official documentation then. Otherwise, implicitly, according to the official documentation, we could customize error messages of all validation rules.

@rcknr
Copy link
Contributor

rcknr commented Nov 26, 2023

@begueradj My changes were released in 10.33.

@begueradj
Copy link
Author

You did a wonderful work @rcknr
Thank you so much for your efforts.
That is a precious contribution you made for the community.
Thank you also for having notified me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants