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 0c1260c

Browse filesBrowse 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
Copy full SHA for 0c1260c

File tree

2 files changed

+30
-0
lines changed
Filter options

2 files changed

+30
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Mapping/ClassMetadata.php
+4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php
+26Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.