Skip to content

Commit 310fc6a

Browse files
committed
feature symfony#58062 [Validator] Add $groups and $payload to Compound constructor (derrabus)
This PR was merged into the 7.2 branch. Discussion ---------- [Validator] Add $groups and $payload to Compound constructor | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | N/A | License | MIT While reviewing symfony#49547 I noticed that the `$groups` and `$payload` parameters introduced in symfony#41994 cannot be leveraged in compound constraints. I'd like to change that. Commits ------- bf4207c [Validator] Add $groups and $payload to Compound constructor
2 parents f9b47a0 + bf4207c commit 310fc6a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ abstract class Compound extends Composite
2424
/** @var Constraint[] */
2525
public array $constraints = [];
2626

27-
public function __construct(mixed $options = null)
27+
public function __construct(mixed $options = null, ?array $groups = null, mixed $payload = null)
2828
{
2929
if (isset($options[$this->getCompositeOption()])) {
3030
throw new ConstraintDefinitionException(\sprintf('You can\'t redefine the "%s" option. Use the "%s::getConstraints()" method instead.', $this->getCompositeOption(), __CLASS__));
3131
}
3232

3333
$this->constraints = $this->getConstraints($this->normalizeOptions($options));
3434

35-
parent::__construct($options);
35+
parent::__construct($options, $groups, $payload);
3636
}
3737

3838
final protected function getCompositeOption(): string

src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ public function testItCannotRedefineConstraintsOption()
2626
new EmptyCompound(['constraints' => [new NotBlank()]]);
2727
}
2828

29+
public function testGroupsAndPayload()
30+
{
31+
$payload = new \stdClass();
32+
$compound = new EmptyCompound(groups: ['my-group', 'my-other-group'], payload: $payload);
33+
34+
$this->assertSame(['my-group', 'my-other-group'], $compound->groups);
35+
$this->assertSame($payload, $compound->payload);
36+
}
37+
38+
public function testGroupsAndPayloadInOptionsArray()
39+
{
40+
$payload = new \stdClass();
41+
$compound = new EmptyCompound(['groups' => ['my-group', 'my-other-group'], 'payload' => $payload]);
42+
43+
$this->assertSame(['my-group', 'my-other-group'], $compound->groups);
44+
$this->assertSame($payload, $compound->payload);
45+
}
46+
2947
public function testCanDependOnNormalizedOptions()
3048
{
3149
$constraint = new ForwardingOptionCompound($min = 3);

0 commit comments

Comments
 (0)