Posted: 21-05-2021

Laravel 8 Password rules

Laravel 8 Password rules

Working on some new features in maguttiCms, I've recently implemented the new Laravel Password rule object in order to ensure that user's passwords have an adequate level of complexity.


Basic Implementation

The Password rule object  implements the following methods:

// Require at least 8 characters...

// Require at least one letter...

// Require at least one uppercase and one lowercase letter...

// Require at least one number...

// Require at least one symbol...

Of course, you may chain all the methods in the examples above so you can write a validation rule like this:


In addition, you may add an extra layer of security to ensure that a password has not been compromised in a public password data breach leak using the uncompromised method:


Internally, the Password rule object uses the k-Anonymity model to determine if a password has been leaked via the service without sacrificing the user's privacy or security.

// Ensure the password appears less than 3 times in the same data leak...

Use the same password validation rules in different places

In an application usually we use the same password validation rules in different places, so you can find convenient define it in a single place.

You can easily accomplish this using the Password::defaults method, which accepts a closure. The closure given to the defaults method should return the default configuration of the Password rule. Typically, the defaults rule should be called within the boot method of one of your application's service providers:

use Illuminate\Validation\Rules\Password;

 * Bootstrap in your application service provider.
 * @return void
public function boot()
    Password::defaults(function () {
        $rule = Password::min(8);

        return $this->app->isProduction()
                    ? $rule->mixedCase()->uncompromised()
                    : $rule;


You may invoke the defaults method  and re-use the validation rules in this way:

'password' => ['required', Password:defaults()]

Localize the Password validation errors message

By default the Password validation errors message are written in English.

If you need to localize them you may be defined within JSON files that are placed within the resources/lang 


Hereafter an example:
You can find the validation rules translation's in json format  on Laravel Lang package 

For more details about the Password rules see the offical Laravel documentation.