Skip to content

Commit 0c1260c

Browse files
committed
bug #21053 [Validator] override property constraints in child class (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Validator] override property constraints in child class | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15950 | License | MIT | Doc PR | Commits ------- 8b281fe override property constraints in child class
2 parents a4ac1a7 + 8b281fe commit 0c1260c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ public function mergeConstraints(ClassMetadata $source)
346346
}
347347

348348
foreach ($source->getConstrainedProperties() as $property) {
349+
if ($this->hasPropertyMetadata($property)) {
350+
continue;
351+
}
352+
349353
foreach ($source->getPropertyMetadata($property) as $member) {
350354
$member = clone $member;
351355

src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Constraints\GreaterThan;
1516
use Symfony\Component\Validator\Constraints\Valid;
1617
use Symfony\Component\Validator\Mapping\ClassMetadata;
1718
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -295,4 +296,29 @@ public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadat
295296
{
296297
$this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
297298
}
299+
300+
public function testMergeDoesOverrideConstraintsFromParentClassIfPropertyIsOverriddenInChildClass()
301+
{
302+
$parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass');
303+
$parentMetadata->addPropertyConstraint('example', new GreaterThan(0));
304+
305+
$childMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass');
306+
$childMetadata->addPropertyConstraint('example', new GreaterThan(1));
307+
$childMetadata->mergeConstraints($parentMetadata);
308+
309+
$expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass');
310+
$expectedMetadata->addPropertyConstraint('example', new GreaterThan(1));
311+
312+
$this->assertEquals($expectedMetadata, $childMetadata);
313+
}
314+
}
315+
316+
class ParentClass
317+
{
318+
public $example = 0;
319+
}
320+
321+
class ChildClass extends ParentClass
322+
{
323+
public $example = 1; // overrides parent property of same name
298324
}

0 commit comments

Comments
 (0)