From 2eed59db0d5297f9b29227b3929b516f4de0e11c Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Tue, 13 Jun 2023 22:27:53 +0200 Subject: [PATCH] [Validator] Add the `message` option to the `PasswordStrength` constraint --- .../Component/Validator/Constraints/PasswordStrength.php | 3 ++- .../Validator/Tests/Constraints/PasswordStrengthTest.php | 3 ++- .../Tests/Constraints/PasswordStrengthValidatorTest.php | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/PasswordStrength.php b/src/Symfony/Component/Validator/Constraints/PasswordStrength.php index b41965879f5eb..816708d67ea91 100644 --- a/src/Symfony/Component/Validator/Constraints/PasswordStrength.php +++ b/src/Symfony/Component/Validator/Constraints/PasswordStrength.php @@ -40,13 +40,14 @@ final class PasswordStrength extends Constraint public int $minScore; - public function __construct(array $options = null, int $minScore = null, array $groups = null, mixed $payload = null) + public function __construct(array $options = null, int $minScore = null, array $groups = null, mixed $payload = null, string $message = null) { $options['minScore'] ??= self::STRENGTH_MEDIUM; parent::__construct($options, $groups, $payload); $this->minScore = $minScore ?? $this->minScore; + $this->message = $message ?? $this->message; if ($this->minScore < 1 || 4 < $this->minScore) { throw new ConstraintDefinitionException(sprintf('The parameter "minScore" of the "%s" constraint must be an integer between 1 and 4.', self::class)); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthTest.php b/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthTest.php index 2cfe6a3abb697..713c30deec7cf 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthTest.php @@ -25,9 +25,10 @@ public function testConstructor() public function testConstructorWithParameters() { - $constraint = new PasswordStrength(minScore: PasswordStrength::STRENGTH_STRONG); + $constraint = new PasswordStrength(minScore: PasswordStrength::STRENGTH_STRONG, message: 'This password should be strong.'); $this->assertSame(PasswordStrength::STRENGTH_STRONG, $constraint->minScore); + $this->assertSame('This password should be strong.', $constraint->message); } public function testInvalidScoreOfZero() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthValidatorTest.php index 474dd889c3686..21dabcad738a4 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/PasswordStrengthValidatorTest.php @@ -77,5 +77,11 @@ public static function provideInvalidConstraints(): iterable 'The password strength is too low. Please use a stronger password.', PasswordStrength::PASSWORD_STRENGTH_ERROR, ]; + yield [ + new PasswordStrength(message: 'This password should be strong.'), + 'password', + 'This password should be strong.', + PasswordStrength::PASSWORD_STRENGTH_ERROR, + ]; } }