8000 [Validator] Allow Nested Valid Constraint by rodnaph · Pull Request #49117 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Allow Nested Valid Constraint #49117

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000 Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Validator] allow nested Valid constraint
removes the restriction that the Valid constraint cannot be nested
inside a composite constraint.
  • Loading branch information
rodnaph committed Jan 26, 2023
commit 89698b98a54ef3c7cb8137ba055688784fea4ce5
6 changes: 1 addition & 5 deletions src/Symfony/Component/Validator/Constraints/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,13 @@ public function __construct(mixed $options = null, array $groups = null, mixed $

throw new ConstraintDefinitionException(sprintf('The value "%s" is not an instance of Constraint in constraint "%s".', $constraint, static::class));
}

if ($constraint instanceof Valid) {
throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint "%s". You can only declare the Valid constraint directly on a field or method.', static::class));
}
}

if (!isset(((array) $this)['groups'])) {
$mergedGroups = [];

foreach ($nestedConstraints as $constraint) {
foreach ($constraint->groups as $group) {
foreach ($constraint->groups ?? [self::DEFAULT_GROUP] as $group) {
$mergedGroups[$group] = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public function testFailIfNoConstraintObject()
]);
}

public function testValidCantBeNested()
public function testValidCanBeNested()
{
$this->expectException(ConstraintDefinitionException::class);
new ConcreteComposite([
new Valid(),
]);
$nestedConstraint = new Valid();
$constraint = new ConcreteComposite($nestedConstraint);

$this->assertEquals([$nestedConstraint], $constraint->constraints);
}
}
0