Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] Fix traverse option on Valid constraint when used as Attribute #46244

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
Jul 15, 2022
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
[Validator] Fix traverse option on Valid constraint when used as Attr…
…ibute
  • Loading branch information
tobias-93 authored and fabpot committed Jul 15, 2022
commit ebbe722068bb653889a82758c0fc08e252a50aa2
7 changes: 7 additions & 0 deletions 7 src/Symfony/Component/Validator/Constraints/Valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class Valid extends Constraint
{
public $traverse = true;

public function __construct(array $options = null, array $groups = null, $payload = null, bool $traverse = null)
{
parent::__construct($options ?? [], $groups, $payload);

$this->traverse = $traverse ?? $this->traverse;
}

public function __get(string $option)
{
if ('groups' === $option) {
Expand Down
32 changes: 32 additions & 0 deletions 32 src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -32,4 +34,34 @@ public function testGroupsAreNullByDefault()

$this->assertNull($constraint->groups);
}

/**
* @requires PHP 8
*/
public function testAttributes()
{
$metadata = new ClassMetaData(ValidDummy::class);
$loader = new AnnotationLoader();
self::assertTrue($loader->loadClassMetadata($metadata));

[$bConstraint] = $metadata->properties['b']->getConstraints();
self::assertFalse($bConstraint->traverse);
self::assertSame(['traverse_group'], $bConstraint->groups);

[$cConstraint] = $metadata->properties['c']->getConstraints();
self::assertSame(['my_group'], $cConstraint->groups);
self::assertSame('some attached data', $cConstraint->payload);
}
}

class ValidDummy
{
#[Valid]
private $a;

#[Valid(groups: ['traverse_group'], traverse: false)] // Needs a group to work at all for this test
xabbuh marked this conversation as resolved.
Show resolved Hide resolved
private $b;

#[Valid(groups: ['my_group'], payload: 'some attached data')]
private $c;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.