Skip to content

[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
7 changes: 7 additions & 0 deletions 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 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
private $b;

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

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy