Defining Default Password Validation Rules in Laravel 8

Defining Default Password Validation Rules in Laravel 8

The Password Validation Rule Object in Laravel 8.43 allows you to set default password rules that may be used across your application. Setting default password rules enables you to centralize the expected password validation behavior by defining them in a service provider.

use IlluminateValidationRulesPassword;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Password::defaults(function () {
return Password::min(8)
->mixedCase()
->uncompromised();
});
}
The defaults are saved, and the Password::defaults() function may be used to obtain them later in a validator:

use IlluminateValidationRulesPassword;

$request->validate([
‘password’ => [‘required’, Password::defaults()],
]);
The validation rule for passwords offers practical password conventions to ensure strong passwords, including verifying if the password has been stolen in known data dumps.