Skip to content

Commit fa61d6e

Browse files
feature #45371 [Validator] Deprecate Constraint::$errorNames in favor of Constraint::ERROR_NAMES (nicolas-grekas)
This PR was merged into the 6.1 branch. Discussion ---------- [Validator] Deprecate `Constraint::$errorNames` in favor of `Constraint::ERROR_NAMES` | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - On the road to #43600 The BC layer is a bit light, but since this property seems like a piece of declarative statement that should we accessed only through `Contraint::getErrorName()`, I suggest doing it this way, and improve only if/when we get feedback from userland about it. Commits ------- aed0b99 [Validator] Deprecate `Constraint::$errorNames` in favor of `Constraint::ERROR_NAMES`
2 parents 34a265c + aed0b99 commit fa61d6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+362
-58
lines changed

UPGRADE-6.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ Serializer
1313
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
1414
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
1515
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
16+
17+
Validator
18+
---------
19+
20+
* Deprecate `Constraint::$errorNames`, use `Constraint::ERROR_NAMES` instead

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class UniqueEntity extends Constraint
2626
{
2727
public const NOT_UNIQUE_ERROR = '23bd9dbf-6b9b-41cd-a99e-4844bcf3077f';
2828

29+
protected const ERROR_NAMES = [
30+
self::NOT_UNIQUE_ERROR => 'NOT_UNIQUE_ERROR',
31+
];
32+
2933
public $message = 'This value is already used.';
3034
public $service = 'doctrine.orm.validator.unique';
3135
public $em = null;
@@ -35,9 +39,10 @@ class UniqueEntity extends Constraint
3539
public $errorPath = null;
3640
public $ignoreNull = true;
3741

38-
protected static $errorNames = [
39-
self::NOT_UNIQUE_ERROR => 'NOT_UNIQUE_ERROR',
40-
];
42+
/**
43+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
44+
*/
45+
protected static $errorNames = self::ERROR_NAMES;
4146

4247
/**
4348
* {@inheritdoc}

src/Symfony/Component/Form/Extension/Validator/Constraints/Form.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ class Form extends Constraint
2121
public const NOT_SYNCHRONIZED_ERROR = '1dafa156-89e1-4736-b832-419c2e501fca';
2222
public const NO_SUCH_FIELD_ERROR = '6e5212ed-a197-4339-99aa-5654798a4854';
2323

24-
protected static $errorNames = [
24+
protected const ERROR_NAMES = [
2525
self::NOT_SYNCHRONIZED_ERROR => 'NOT_SYNCHRONIZED_ERROR',
2626
self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
2727
];
2828

29+
/**
30+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
31+
*/
32+
protected static $errorNames = self::ERROR_NAMES;
33+
2934
/**
3035
* {@inheritdoc}
3136
*/

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Deprecate `Constraint::$errorNames`, use `Constraint::ERROR_NAMES` instead
8+
49
6.0
510
---
611

src/Symfony/Component/Validator/Constraint.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ abstract class Constraint
4747
/**
4848
* Maps error codes to the names of their constants.
4949
*/
50+
protected const ERROR_NAMES = [];
51+
52+
/**
53+
* @deprecated since Symfony 6.1, use protected const ERROR_NAMES instead
54+
*/
5055
protected static $errorNames = [];
5156

5257
/**
@@ -70,10 +75,16 @@ abstract class Constraint
7075
*/
7176
public static function getErrorName(string $errorCode): string
7277
{
78+
if (isset(static::ERROR_NAMES[$errorCode])) {
79+
return static::ERROR_NAMES[$errorCode];
80+
}
81+
7382
if (!isset(static::$errorNames[$errorCode])) {
7483
throw new InvalidArgumentException(sprintf('The error code "%s" does not exist for constraint of type "%s".', $errorCode, static::class));
7584
}
7685

86+
trigger_deprecation('symfony/validator', '6.1', 'The "%s::$errorNames" property is deprecated, use protected const ERROR_NAMES instead.', static::class);
87+
7788
return static::$errorNames[$errorCode];
7889
}
7990

src/Symfony/Component/Validator/Constraints/AtLeastOneOf.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ class AtLeastOneOf extends Composite
2222
{
2323
public const AT_LEAST_ONE_OF_ERROR = 'f27e6d6c-261a-4056-b391-6673a623531c';
2424

25-
protected static $errorNames = [
25+
protected const ERROR_NAMES = [
2626
self::AT_LEAST_ONE_OF_ERROR => 'AT_LEAST_ONE_OF_ERROR',
2727
];
2828

29+
/**
30+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
31+
*/
32+
protected static $errorNames = self::ERROR_NAMES;
33+
2934
public $constraints = [];
3035
public $message = 'This value should satisfy at least one of the following constraints:';
3136
public $messageCollection = 'Each element of this collection should satisfy its own set of constraints.';

src/Symfony/Component/Validator/Constraints/Bic.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ class Bic extends Constraint
3333
public const INVALID_CASE_ERROR = '11884038-3312-4ae5-9d04-699f782130c7';
3434
public const INVALID_IBAN_COUNTRY_CODE_ERROR = '29a2c3bb-587b-4996-b6f5-53081364cea5';
3535

36-
protected static $errorNames = [
36+
protected const ERROR_NAMES = [
3737
self::INVALID_LENGTH_ERROR => 'INVALID_LENGTH_ERROR',
3838
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
3939
self::INVALID_BANK_CODE_ERROR => 'INVALID_BANK_CODE_ERROR',
4040
self::INVALID_COUNTRY_CODE_ERROR => 'INVALID_COUNTRY_CODE_ERROR',
4141
self::INVALID_CASE_ERROR => 'INVALID_CASE_ERROR',
4242
];
4343

44+
/**
45+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
46+
*/
47+
protected static $errorNames = self::ERROR_NAMES;
48+
4449
public $message = 'This is not a valid Business Identifier Code (BIC).';
4550
public $ibanMessage = 'This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.';
4651
public $iban;

src/Symfony/Component/Validator/Constraints/Blank.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ class Blank extends Constraint
2424
{
2525
public const NOT_BLANK_ERROR = '183ad2de-533d-4796-a439-6d3c3852b549';
2626

27-
protected static $errorNames = [
27+
protected const ERROR_NAMES = [
2828
self::NOT_BLANK_ERROR => 'NOT_BLANK_ERROR',
2929
];
3030

31+
/**
32+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
33+
*/
34+
protected static $errorNames = self::ERROR_NAMES;
35+
3136
public $message = 'This value should be blank.';
3237

3338
public function __construct(array $options = null, string $message = null, array $groups = null, mixed $payload = null)

src/Symfony/Component/Validator/Constraints/CardScheme.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ class CardScheme extends Constraint
4141
public const NOT_NUMERIC_ERROR = 'a2ad9231-e827-485f-8a1e-ef4d9a6d5c2e';
4242
public const INVALID_FORMAT_ERROR = 'a8faedbf-1c2f-4695-8d22-55783be8efed';
4343

44-
protected static $errorNames = [
44+
protected const ERROR_NAMES = [
4545
self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR',
4646
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
4747
];
4848

49+
/**
50+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
51+
*/
52+
protected static $errorNames = self::ERROR_NAMES;
53+
4954
public $message = 'Unsupported card type or invalid card number.';
5055
public $schemes;
5156

src/Symfony/Component/Validator/Constraints/Choice.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ class Choice extends Constraint
2626
public const TOO_FEW_ERROR = '11edd7eb-5872-4b6e-9f12-89923999fd0e';
2727
public const TOO_MANY_ERROR = '9bd98e49-211c-433f-8630-fd1c2d0f08c3';
2828

29-
protected static $errorNames = [
29+
protected const ERROR_NAMES = [
3030
self::NO_SUCH_CHOICE_ERROR => 'NO_SUCH_CHOICE_ERROR',
3131
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
3232
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
3333
];
3434

35+
/**
36+
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
37+
*/
38+
protected static $errorNames = self::ERROR_NAMES;
39+
3540
public $choices;
3641
public $callback;
3742
public $multiple = false;

0 commit comments

Comments
 (0)