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

Commit 550e4cc

Browse filesBrowse files
committed
bug #46244 [Validator] Fix traverse option on Valid constraint when used 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
2 parents d72da69 + ebbe722 commit 550e4cc
Copy full SHA for 550e4cc

File tree

2 files changed

+39
-0
lines changed
Filter options

2 files changed

+39
-0
lines changed

‎src/Symfony/Component/Validator/Constraints/Valid.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Valid.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class Valid extends Constraint
2424
{
2525
public $traverse = true;
2626

27+
public function __construct(array $options = null, array $groups = null, $payload = null, bool $traverse = null)
28+
{
29+
parent::__construct($options ?? [], $groups, $payload);
30+
31+
$this->traverse = $traverse ?? $this->traverse;
32+
}
33+
2734
public function __get(string $option)
2835
{
2936
if ('groups' === $option) {

‎src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraints\Valid;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
1618

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

3335
$this->assertNull($constraint->groups);
3436
}
37+
38+
/**
39+
* @requires PHP 8
40+
*/
41+
public function testAttributes()
42+
{
43+
$metadata = new ClassMetaData(ValidDummy::class);
44+
$loader = new AnnotationLoader();
45+
self::assertTrue($loader->loadClassMetadata($metadata));
46+
47+
[$bConstraint] = $metadata->properties['b']->getConstraints();
48+
self::assertFalse($bConstraint->traverse);
49+
self::assertSame(['traverse_group'], $bConstraint->groups);
50+
51+
[$cConstraint] = $metadata->properties['c']->getConstraints();
52+
self::assertSame(['my_group'], $cConstraint->groups);
53+
self::assertSame('some attached data', $cConstraint->payload);
54+
}
55+
}
56+
57+
class ValidDummy
58+
{
59+
#[Valid]
60+
private $a;
61+
62+
#[Valid(groups: ['traverse_group'], traverse: false)] // Needs a group to work at all for this test
63+
private $b;
64+
65+
#[Valid(groups: ['my_group'], payload: 'some attached data')]
66+
private $c;
3567
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.