Skip to content

Commit

Permalink
bug #46244 [Validator] Fix traverse option on Valid constraint when u…
Browse files Browse the repository at this point in the history
…sed as Attribute (tobias-93)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46243
| License       | MIT
| Doc PR        | n.a.

Fix Valid constraint when using as Attribute.

Commits
-------

ebbe722 [Validator] Fix traverse option on Valid constraint when used as Attribute
  • Loading branch information
fabpot committed Jul 15, 2022
2 parents d72da69 + ebbe722 commit 550e4cc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Valid.php
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 src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php
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
private $b;

#[Valid(groups: ['my_group'], payload: 'some attached data')]
private $c;
}

0 comments on commit 550e4cc

Please sign in to comment.