From 72666bb8298ac990094777227d585f2b071a29ad Mon Sep 17 00:00:00 2001 From: dwgebler Date: Thu, 15 Jun 2023 23:16:26 +0100 Subject: [PATCH 1/5] Validate all groups when special group name "*" is specified in validate() --- .../Validator/Mapping/GenericMetadata.php | 7 ++++++ .../Validator/RecursiveValidatorTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php index f05e402609d5d..81d13ad3838b6 100644 --- a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php @@ -204,6 +204,13 @@ public function hasConstraints(): bool */ public function findConstraints(string $group): array { + if ('*' === $group) { + $constraints = []; + foreach ($this->constraintsByGroup as $groupConstraints) { + $constraints = array_merge($constraints, $groupConstraints); + } + return $constraints; + } return $this->constraintsByGroup[$group] ?? []; } diff --git a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php index 010536e661f19..b0c1eb3105b88 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php @@ -1114,6 +1114,29 @@ public function testValidateMultipleGroups() $this->assertCount(2, $violations); } + public function testValidateAllGroups() + { + $entity = new Entity(); + + $callback = function ($value, ExecutionContextInterface $context) { + $context->addViolation('Message'); + }; + + $this->metadata->addConstraint(new Callback([ + 'callback' => $callback, + 'groups' => 'Group 1', + ])); + $this->metadata->addConstraint(new Callback([ + 'callback' => $callback, + 'groups' => 'Group 2', + ])); + + $violations = $this->validate($entity, null, '*'); + + /* @var ConstraintViolationInterface[] $violations */ + $this->assertCount(2, $violations); + } + public function testReplaceDefaultGroupByGroupSequenceObject() { $entity = new Entity(); From 2f67a5909a65e2c0b524f8e17e31d655adaf634c Mon Sep 17 00:00:00 2001 From: dwgebler Date: Thu, 15 Jun 2023 23:24:40 +0100 Subject: [PATCH 2/5] update validator changelog --- src/Symfony/Component/Validator/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index fc974c5a185a9..e953159c10137 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.4 +--- + + * Add the special group name `*` which can be passed to `ValidatorInterface::validate()` to validate all constraints, regardless of their group. + 6.3 --- From e0aa29be003104e88216443e7609f242518030ac Mon Sep 17 00:00:00 2001 From: dwgebler Date: Thu, 15 Jun 2023 23:38:48 +0100 Subject: [PATCH 3/5] add default group to test case --- src/Symfony/Component/Validator/Mapping/GenericMetadata.php | 2 ++ .../Validator/Tests/Validator/RecursiveValidatorTest.php | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php index 81d13ad3838b6..c59aad824d042 100644 --- a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php @@ -209,8 +209,10 @@ public function findConstraints(string $group): array foreach ($this->constraintsByGroup as $groupConstraints) { $constraints = array_merge($constraints, $groupConstraints); } + return $constraints; } + return $this->constraintsByGroup[$group] ?? []; } diff --git a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php index b0c1eb3105b88..a1d3efe77ad85 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php @@ -1130,11 +1130,14 @@ public function testValidateAllGroups() 'callback' => $callback, 'groups' => 'Group 2', ])); + $this->metadata->addConstraint(new Callback([ + 'callback' => $callback, + ])); $violations = $this->validate($entity, null, '*'); /* @var ConstraintViolationInterface[] $violations */ - $this->assertCount(2, $violations); + $this->assertCount(3, $violations); } public function testReplaceDefaultGroupByGroupSequenceObject() From 5b6b433b8a28e22780363c9fa93873d162095be7 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Thu, 15 Jun 2023 23:16:26 +0100 Subject: [PATCH 4/5] Validate all groups when special group name "*" is specified in validate() --- .../Validator/Tests/Validator/RecursiveValidatorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php index a1d3efe77ad85..93f2ba1919bb6 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php @@ -1136,7 +1136,6 @@ public function testValidateAllGroups() $violations = $this->validate($entity, null, '*'); - /* @var ConstraintViolationInterface[] $violations */ $this->assertCount(3, $violations); } From a279c565db8f63901435fcd5bf3ecf98795ed806 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Thu, 15 Jun 2023 23:16:26 +0100 Subject: [PATCH 5/5] Validate all groups when special group name "*" is specified in validate() --- .../Validator/Tests/Validator/RecursiveValidatorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php index 93f2ba1919bb6..a1d3efe77ad85 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php @@ -1136,6 +1136,7 @@ public function testValidateAllGroups() $violations = $this->validate($entity, null, '*'); + /* @var ConstraintViolationInterface[] $violations */ $this->assertCount(3, $violations); }