deniak/password-strength-bundle
Installation
composer require deniak/password-strength-bundle
Add to config/bundles.php:
return [
// ...
Deniak\PasswordStrengthBundle\DeniakPasswordStrengthBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan vendor:publish --tag=password-strength-config
Edit config/packages/deniak_password_strength.yaml to define rules (e.g., min_length: 12).
First Use Case Validate a password in a Symfony Form:
use Deniak\PasswordStrengthBundle\Validator\Constraints as PasswordAssert;
// In your entity or form type
#[PasswordAssert\PasswordStrength]
private $password;
Or manually:
$validator = $this->get('validator');
$violations = $validator->validate($password, new PasswordStrength());
Form Validation Use the constraint in Symfony Forms or API Platform:
// src/Form/RegistrationType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('password', TextType::class, [
'constraints' => [new PasswordStrength()]
]);
}
API Request Validation Validate incoming JSON payloads (e.g., Laravel Sanctum or API Platform):
use Deniak\PasswordStrengthBundle\Validator\Constraints\PasswordStrength;
// In a request DTO or entity
#[PasswordStrength]
public string $password;
Custom Rules Extend the validator for project-specific needs:
// src/Validator/Constraints/CustomPasswordStrength.php
use Deniak\PasswordStrengthBundle\Validator\Constraints\PasswordStrength as BasePasswordStrength;
#[Attribute]
class CustomPasswordStrength extends BasePasswordStrength {
public function validatedBy(): string {
return get_class($this) . 'Validator';
}
}
Dynamic Rules Pass runtime rules via options:
$validator->validate($password, new PasswordStrength([
'min_length' => 14,
'require_uppercase' => true,
'require_special_chars' => true,
]));
Integration with Laravel Fortify/Passport Hook into registration/login flows:
// app/Providers/FortifyServiceProvider.php
public function register()
{
Fortify::registerView(function () {
return view('auth.register')
->with('passwordRules', config('password-strength.rules'));
});
}
Configuration Overrides
Symfony\Component\Validator\Constraints\Length). Tip: Disable redundant validators in your entity:
#[Assert\Length(min: 8, max: 255)]
#[PasswordAssert\PasswordStrength(min_length: 12)]
private $password;
Unicode/Special Character Handling
$password = 'P@ssw0rd!'; // Valid
$password = 'パスワード!'; // May fail if `require_alphanumeric` is strict
Case Sensitivity in Tests
require_uppercase are case-sensitive. Tip: Use regex to pre-validate:
if (!preg_match('/[A-Z]/', $password)) {
$violations->add(new ConstraintViolation(...));
}
Performance with Large Datasets
Bundle Compatibility
use Deniak\PasswordStrengthBundle\Validator\PasswordStrengthValidator;
Validator::extend('password_strength', function ($attribute, $value, $parameters, $validator) {
$validator = new PasswordStrengthValidator();
$constraint = new PasswordStrength($parameters);
$violations = $validator->validate($value, $constraint);
return $violations->count() === 0;
});
$violations = $validator->validate($password, new PasswordStrength());
foreach ($violations as $violation) {
dump($violation->getPropertyPath(), $violation->getMessage());
}
debug: true in config/packages/deniak_password_strength.yaml to log detailed validation steps.Custom Validators Override the default validator:
// src/Validator/Constraints/PasswordStrengthValidator.php
class CustomPasswordStrengthValidator extends PasswordStrengthValidator {
protected function validateLength($value, $constraint) {
// Custom logic
}
}
Translation Messages
Extend translations in resources/lang/en/validation.php:
'password' => [
'strength' => [
'min_length' => 'The :attribute must be at least :min characters.',
'require_uppercase' => 'The :attribute must contain at least one uppercase letter.',
],
],
Integration with Laravel Hashing
Combine with Laravel’s Hash::needsRehash() for password updates:
if (Hash::needsRehash($user->password) && !PasswordStrengthValidator::isStrong($newPassword)) {
// Reject update
}
How can I help you explore Laravel packages today?