The LaraUtilX package provides custom validation rules to enhance the security and data integrity of your Laravel applications.
The RejectCommonPasswords validation rule helps improve password security by rejecting commonly used passwords that are easily guessable or vulnerable to brute force attacks.
The rule rejects passwords including but not limited to:
123456, qwerty, abc123password, admin, welcome, helloqwertyuiop, asdfghjkl, zxcvbnm111111, aaaa, 000000password1, admin123, qwerty123use LaraUtilX\Rules\RejectCommonPasswords;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$request->validate([
'password' => ['required', 'string', 'min:8', new RejectCommonPasswords()],
]);
// Password is valid and not a common password
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use LaraUtilX\Rules\RejectCommonPasswords;
class CreateUserRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => [
'required',
'string',
'min:8',
'confirmed',
new RejectCommonPasswords()
],
];
}
}
The package also registers a custom validation rule string for convenience:
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$request->validate([
'password' => 'required|string|min:8|reject_common_passwords',
]);
// Password is valid and not a common password
}
}
use LaraUtilX\Rules\RejectCommonPasswords;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function testPasswordValidation(Request $request)
{
$validator = Validator::make($request->all(), [
'password' => ['required', 'string', new RejectCommonPasswords()],
]);
if ($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $validator->errors()
], 422);
}
return response()->json([
'success' => true,
'message' => 'Password passed validation - it is not a common password'
]);
}
}
The RejectCommonPasswords rule is automatically registered when the package is installed. No additional configuration is required.
If you need to customize the list of common passwords, you can extend the rule:
<?php
namespace App\Rules;
use LaraUtilX\Rules\RejectCommonPasswords as BaseRejectCommonPasswords;
class CustomRejectCommonPasswords extends BaseRejectCommonPasswords
{
protected array $commonPasswords = [
// Your custom list of common passwords
'company123',
'mypassword',
'secret123',
// ... add more as needed
];
}
The default error message is:
The :attribute contains a common password that is not allowed.
You can customize the error message in several ways:
public function messages(): array
{
return [
'password.reject_common_passwords' => 'Please choose a more secure password that is not commonly used.',
];
}
$request->validate([
'password' => ['required', 'string', new RejectCommonPasswords()],
], [
'password.reject_common_passwords' => 'Your password is too common. Please choose a more unique password.',
]);
Create or update resources/lang/en/validation.php:
return [
'reject_common_passwords' => 'The :attribute contains a common password that is not allowed.',
// ... other validation messages
];
You can publish the validation rule to your application's app/Rules directory:
php artisan vendor:publish --tag=lara-util-x-validation-rules
This will create a copy of the RejectCommonPasswords rule in your app/Rules directory, allowing you to customize it as needed.
After publishing, you'll find:
app/
└── Rules/
└── RejectCommonPasswords.php
min:8, confirmed, and other password rules<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use LaraUtilX\Rules\RejectCommonPasswords;
class RegisterRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => [
'required',
'string',
'min:8',
'confirmed',
'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/',
new RejectCommonPasswords()
],
];
}
public function messages(): array
{
return [
'password.reject_common_passwords' => 'Please choose a more secure password that is not commonly used.',
'password.regex' => 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character.',
];
}
}
use LaraUtilX\Rules\RejectCommonPasswords;
class PasswordController extends Controller
{
public function validatePassword(Request $request)
{
$validator = Validator::make($request->all(), [
'password' => ['required', 'string', 'min:8', new RejectCommonPasswords()],
]);
if ($validator->fails()) {
return response()->json([
'valid' => false,
'errors' => $validator->errors()
], 422);
}
return response()->json([
'valid' => true,
'message' => 'Password meets security requirements'
]);
}
}
This validation rule is an essential tool for improving password security in your Laravel applications, helping protect your users and data from common password-based attacks.