From 574dfdc3e2e773d61c3190a9bce2471b5e12214f Mon Sep 17 00:00:00 2001 From: catch Date: Thu, 14 Mar 2024 21:30:16 +0000 Subject: [PATCH] Issue #3427737 by longwave, godotislate: Overwrite Symfony constraint properties in constructors instead of child properties (cherry picked from commit f0071e2477584a8560f088c80bb302e2d3335c9b) --- core/.deprecation-ignore.txt | 4 ---- .../Constraint/AllowedValuesConstraint.php | 12 +++++++++--- .../Validation/Constraint/CountConstraint.php | 12 +++++++++--- .../Validation/Constraint/EmailConstraint.php | 6 +++--- .../Validation/Constraint/LengthConstraint.php | 12 +++++++++--- .../Validation/Constraint/RangeConstraint.php | 14 +++++--------- .../Validation/Constraint/RegexConstraint.php | 2 -- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/core/.deprecation-ignore.txt b/core/.deprecation-ignore.txt index 076d302f90d..2fc93ec6bdb 100644 --- a/core/.deprecation-ignore.txt +++ b/core/.deprecation-ignore.txt @@ -27,10 +27,6 @@ # testing using \Symfony\Component\ErrorHandler\DebugClassLoader. %The "Twig\\Environment::getTemplateClass\(\)" method is considered internal\. It may change without further notice\. You should not extend it from "Drupal\\Core\\Template\\TwigEnvironment"\.% -# Skip deprecations warning for overriding Symfony properties in validation -# constraints. See https://www.drupal.org/project/drupal/issues/3425150. -%The "Symfony\\Component\\Validator\\Constraints\\[^"]+::\$[^"]+" property is considered final. You should not override it in "[^"]+"% - # Drupal 11. %Drupal\\Core\\Form\\FormBuilder::getForm\(\).* will require a new "mixed \.\.\. \$args" argument in the next major version of its interface% %Drupal\\Core\\Form\\FormBuilder::submitForm\(\).* will require a new "mixed \.\.\. \$args" argument in the next major version of its interface% diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php index c83979343ed..18acb7abd7b 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php @@ -17,8 +17,14 @@ use Symfony\Component\Validator\Constraints\Choice; )] class AllowedValuesConstraint extends Choice { - public $strict = TRUE; - public $minMessage = 'You must select at least %limit choice.|You must select at least %limit choices.'; - public $maxMessage = 'You must select at most %limit choice.|You must select at most %limit choices.'; + /** + * {@inheritdoc} + */ + public function __construct(...$args) { + $this->strict = TRUE; + $this->minMessage = 'You must select at least %limit choice.|You must select at least %limit choices.'; + $this->maxMessage = 'You must select at most %limit choice.|You must select at most %limit choices.'; + parent::__construct(...$args); + } } diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/CountConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/CountConstraint.php index 575e20ae40d..a973f94f9f3 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/CountConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/CountConstraint.php @@ -18,9 +18,15 @@ use Symfony\Component\Validator\Constraints\Count; )] class CountConstraint extends Count { - public $minMessage = 'This collection should contain %limit element or more.|This collection should contain %limit elements or more.'; - public $maxMessage = 'This collection should contain %limit element or less.|This collection should contain %limit elements or less.'; - public $exactMessage = 'This collection should contain exactly %limit element.|This collection should contain exactly %limit elements.'; + /** + * {@inheritdoc} + */ + public function __construct(...$args) { + $this->minMessage = 'This collection should contain %limit element or more.|This collection should contain %limit elements or more.'; + $this->maxMessage = 'This collection should contain %limit element or less.|This collection should contain %limit elements or less.'; + $this->exactMessage = 'This collection should contain exactly %limit element.|This collection should contain exactly %limit elements.'; + parent::__construct(...$args); + } /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php index 2258df31743..04cdaf03f00 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php @@ -20,9 +20,9 @@ class EmailConstraint extends Email { /** * {@inheritdoc} */ - public function __construct($options = []) { - $options += ['mode' => 'strict']; - parent::__construct($options); + public function __construct(...$args) { + $this->mode = static::VALIDATION_MODE_STRICT; + parent::__construct(...$args); } /** diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/LengthConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/LengthConstraint.php index 8373ba92423..3704d2bcd6c 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/LengthConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/LengthConstraint.php @@ -20,9 +20,15 @@ use Symfony\Component\Validator\Constraints\Length; )] class LengthConstraint extends Length { - public $maxMessage = 'This value is too long. It should have %limit character or less.|This value is too long. It should have %limit characters or less.'; - public $minMessage = 'This value is too short. It should have %limit character or more.|This value is too short. It should have %limit characters or more.'; - public $exactMessage = 'This value should have exactly %limit character.|This value should have exactly %limit characters.'; + /** + * {@inheritdoc} + */ + public function __construct(...$args) { + $this->maxMessage = 'This value is too long. It should have %limit character or less.|This value is too long. It should have %limit characters or less.'; + $this->minMessage = 'This value is too short. It should have %limit character or more.|This value is too short. It should have %limit characters or more.'; + $this->exactMessage = 'This value should have exactly %limit character.|This value should have exactly %limit characters.'; + parent::__construct(...$args); + } /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RangeConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RangeConstraint.php index d05123387f7..86fa440ec8e 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RangeConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RangeConstraint.php @@ -23,15 +23,11 @@ class RangeConstraint extends Range { /** * {@inheritdoc} */ - public function __construct(array $options = NULL) { - if (isset($options['min']) && isset($options['max'])) { - $options['notInRangeMessage'] = $options['notInRangeMessage'] ?? 'This value should be between %min and %max.'; - } - else { - $options['minMessage'] = $options['minMessage'] ?? 'This value should be %limit or more.'; - $options['maxMessage'] = $options['maxMessage'] ?? 'This value should be %limit or less.'; - } - parent::__construct($options); + public function __construct(...$args) { + $this->notInRangeMessage = 'This value should be between %min and %max.'; + $this->minMessage = 'This value should be %limit or more.'; + $this->maxMessage = 'This value should be %limit or less.'; + parent::__construct(...$args); } } diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php index 3665836a528..0ffbb34600a 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RegexConstraint.php @@ -17,8 +17,6 @@ use Symfony\Component\Validator\Constraints\Regex; )] class RegexConstraint extends Regex { - public $message = 'This value is not valid.'; - /** * {@inheritdoc} *