Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Validate all groups when special group name * is specified in validate() method #50679

Open
wants to merge 5 commits into
base: 7.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions 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
---

Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Validator/Mapping/GenericMetadata.php
Expand Up @@ -204,6 +204,15 @@ 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] ?? [];
}

Expand Down
Expand Up @@ -1114,6 +1114,32 @@ 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',
]));
$this->metadata->addConstraint(new Callback([
'callback' => $callback,
]));

$violations = $this->validate($entity, null, '*');

/* @var ConstraintViolationInterface[] $violations */
$this->assertCount(3, $violations);
}

public function testReplaceDefaultGroupByGroupSequenceObject()
{
$entity = new Entity();
Expand Down