Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Add $groups and $payload to Compound constructor #58062

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

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Compound.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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__));
}

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

parent::__construct($options);
parent::__construct($options, $groups, $payload);
}

final protected function getCompositeOption(): string
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down