From bf4207c2b2cba434707bd6f5df39cdee74a9864f Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 22 Aug 2024 12:06:10 +0200 Subject: [PATCH] [Validator] Add $groups and $payload to Compound constructor --- .../Validator/Constraints/Compound.php | 4 ++-- .../Tests/Constraints/CompoundTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Compound.php b/src/Symfony/Component/Validator/Constraints/Compound.php index 2862bca3f927f..ac2b5ac9890ca 100644 --- a/src/Symfony/Component/Validator/Constraints/Compound.php +++ b/src/Symfony/Component/Validator/Constraints/Compound.php @@ -24,7 +24,7 @@ abstract class Compound extends Composite /** @var Constraint[] */ public array $constraints = []; - public function __construct(mixed $options = null) + public function __construct(mixed $options = null, ?array $groups = null, mixed $payload = null) { if (isset($options[$this->getCompositeOption()])) { throw new ConstraintDefinitionException(\sprintf('You can\'t redefine the "%s" option. Use the "%s::getConstraints()" method instead.', $this->getCompositeOption(), __CLASS__)); @@ -32,7 +32,7 @@ public function __construct(mixed $options = null) $this->constraints = $this->getConstraints($this->normalizeOptions($options)); - parent::__construct($options); + parent::__construct($options, $groups, $payload); } final protected function getCompositeOption(): string diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php index d5cf9e9ceecbb..26889a0cc5110 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php @@ -26,6 +26,24 @@ public function testItCannotRedefineConstraintsOption() new EmptyCompound(['constraints' => [new NotBlank()]]); } + public function testGroupsAndPayload() + { + $payload = new \stdClass(); + $compound = new EmptyCompound(groups: ['my-group', 'my-other-group'], payload: $payload); + + $this->assertSame(['my-group', 'my-other-group'], $compound->groups); + $this->assertSame($payload, $compound->payload); + } + + public function testGroupsAndPayloadInOptionsArray() + { + $payload = new \stdClass(); + $compound = new EmptyCompound(['groups' => ['my-group', 'my-other-group'], 'payload' => $payload]); + + $this->assertSame(['my-group', 'my-other-group'], $compound->groups); + $this->assertSame($payload, $compound->payload); + } + public function testCanDependOnNormalizedOptions() { $constraint = new ForwardingOptionCompound($min = 3);