E5F5 [Validator] Target aware constraint support for AnnotationLoader · symfony/symfony@604e6a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 604e6a1

Browse files
committed
[Validator] Target aware constraint support for AnnotationLoader
1 parent 007dfd6 commit 604e6a1

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraints\Callback;
1717
use Symfony\Component\Validator\Constraints\GroupSequence;
1818
use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
19+
use Symfony\Component\Validator\Constraints\TargetAwareConstraintInterface;
1920
use Symfony\Component\Validator\Exception\MappingException;
2021
use Symfony\Component\Validator\Mapping\ClassMetadata;
2122

@@ -51,6 +52,10 @@ public function loadClassMetadata(ClassMetadata $metadata)
5152
} elseif ($constraint instanceof GroupSequenceProvider) {
5253
$metadata->setGroupSequenceProvider(true);
5354
} elseif ($constraint instanceof Constraint) {
55+
if ($constraint instanceof TargetAwareConstraintInterface) {
56+
$constraint->target = $metadata->getClassName();
57+
}
58+
5459
$metadata->addConstraint($constraint);
5560
}
5661

src/Symfony/Component/Validator/Tests/Fixtures/EntityParent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\Validator\Constraints\NotNull;
1515

16+
/**
17+
* @Symfony\Component\Validator\Tests\Fixtures\ConstraintD
18+
*/
1619
class EntityParent
1720
{
1821
protected $firstName;

src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Validator\Mapping\ClassMetadata;
2323
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
2424
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
25+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintD;
2526

2627
class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
2728
{
@@ -89,6 +90,7 @@ public function testLoadParentClassMetadata()
8990
$loader->loadClassMetadata($parent_metadata);
9091

9192
$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
93+
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
9294
$expected_parent->addPropertyConstraint('other', new NotNull());
9395
$expected_parent->getReflectionClass();
9496

@@ -113,6 +115,7 @@ public function testLoadClassMetadataAndMerge()
113115
$loader->loadClassMetadata($metadata);
114116

115117
$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
118+
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
116119
$expected_parent->addPropertyConstraint('other', new NotNull());
117120
$expected_parent->getReflectionClass();
118121

0 commit comments

Comments
 (0)
0