Home > front end >  Laravel Validation of password failed, when it must be ok. Why?
Laravel Validation of password failed, when it must be ok. Why?

Time:09-29

$validator = Validator::make($request->all(), [
                'email' => 'required|min:9|max:50|email',
                'password' => 'required|min:8|max:255|password'
            ]);

            if ($validator->fails()) {
                echo "Failed! Sorry :(";
                Session::flash('error', $validator->messages()->first());
                //return Redirect::back()->withErrors($validator);
            }
            else {...

But if I write

'password' => 'required|min:8|max:255' 

the validation is passed, but when I write

'password' => 'required|min:8|max:255|password'  

it doesn't pass and the result is "Failed! Sorry :(". I mean that works the echo which I wrote above.
What can be the problem? Or the "password" is excessive? I am newbie in Laravel.

Important! The password in database is being saved hashed.

CodePudding user response:

The password validation rule checks the field under validation against the current authenticated user's password. So you shouldn't use it if you want to check only it's length. If you want more specific validation rules, you can use regex. For example, this rule checks if the given password contains at least one small letter, one capital letter and a digit and it's length shouldn't be less than 8:

'password' => 'required|min:8|regex:#(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])#'
  • Related