jakubjv's avatar

Custom validation message for password

Hello everyone, have anybody idea how to show custom validation message for mixedCase()->symbols()->numbers()->uncompromises() methods in validation of password?

i did try it like this

if (Auth::user() && Auth::user()->isAdmin) {
            $validatedData = $this->validate([
                'name' => 'required|string|max:250',
                'email' => 'required|email|max:250|unique:users',
                'password' => ['required', 'confirmed', Password::min(8)->mixedCase()->numbers()->symbols()->uncompromised()],
                'isAdmin' => 'required|nullable|in:0,1',
            ], [
                'name.required' => 'Jméno je povinné pole.',
                'email.required' => 'Email je povinné pole.',
                'email.unique' => 'Tento email je již registrován.',
                'password.required' => 'Heslo je povinné pole.',
                'password.min' => 'Heslo musí mít alespoň 8 znaků.',
                'password.confirmed' => 'Hesla se neshodují.',
                'password.mixedCase' => 'Heslo musí obsahovat kombinaci velkých a malých písmen.',
                'password.numbers' => 'Heslo musí obsahovat alespoň jedno číslo.',
                'password.symbols' => 'Heslo musí obsahovat alespoň jeden symbol.',
                'password.uncompromised' => 'Heslo je příliš jednoduché nebo často používané. Zvolte prosím bezpečnější heslo.',
                'isAdmin.required' => 'Admin pole je povinné.',
            ]);

but its not working for me, also i did try to create json file with translations like this

{
    "password.mixedCase": "Heslo musí obsahovat kombinaci velkých a malých písmen.",
    "password.numbers": "Heslo musí obsahovat alespoň jedno číslo.",
    "password.symbols": "Heslo musí obsahovat alespoň jeden symbol.",
    "password.uncompromised": "Heslo je příliš jednoduché nebo často používané. Zvolte prosím bezpečnější heslo."
}

but it also doesn't work for me, have anybody idea how to it? Thanks for advices! :)

0 likes
7 replies
jakubjv's avatar

@martinbean specify where to crate that file? ..should i create it in resources/lang ? i got it but i dont know how to use it

jaseofspades88's avatar

I believe the override is password.mixed. I have confirmed this by looking into the Password validation class you're using. When adding the failure to the message bag, it does so here...

$validator->addFailure($attribute, 'password.mixed');

Furthermore... the following I tested in Tinkerwell...

$validator = Validator::make([
  'password' => 'password',
],[
  'password' => [
    Password::min(8)->mixedCase()
  ]
], [
  'password.mixed' => 'Not mixed case',
]);
jaseofspades88's avatar

What key do you see being attributed to the failure within the mixedCase function on the password class?

jakubjv's avatar

@jaseofspades88 oh sorry,, i don't use validator this way, i am using $this->validate() as above in my first message, i am using livewire

Please or to participate in this conversation.