10000 bug #21592 [Validator] property constraints can be added in child cla… · symfony/symfony@5c07ffa · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c07ffa

Browse files
committed
bug #21592 [Validator] property constraints can be added in child classes (angelk, xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Validator] property constraints can be added in child classes | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21538, #21567 | License | MIT | Doc PR | This reverts the changes done in #21053 (and applies the test written by @angelk in #21538). I think trying to "fix" #15950 with the changes from #21053 was a mistake. Child classes should be able to refine validation constraints (I basically agree with @ro0NL's reasoning in #21567 (comment). Commits ------- 9513a8a property constraints can be added in child classes a266ff7 added test for staticClassLoader in LazyLoadingMetadatafactory
2 parents 2c302ce + 9513a8a commit 5c07ffa

File tree

6 files changed

+90
-20
lines changed

6 files changed

+90
-20
lines changed

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

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

348348
foreach ($source->getConstrainedProperties() as $property) {
349-
if ($this->hasPropertyMetadata($property)) {
350-
continue;
351-
}
352-
353349
foreach ($source->getPropertyMetadata($property) as $member) {
354350
$member = clone $member;
355351

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
use Symfony\Component\Validator\Mapping\ClassMetadata;
15+
use Symfony\Component\Validator\Constraints\Length;
16+
17+
class EntityStaticCar extends EntityStaticVehicle
18+
{
19+
public static function loadValidatorMetadata(ClassMetadata $metadata)
20+
{
21+
$metadata->addPropertyConstraint('wheels', new Length(array('max' => 99)));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
use Symfony\Component\Validator\Mapping\ClassMetadata;
15+
use Symfony\Component\Validator\Constraints\Length;
16+
17+
class EntityStaticCarTurbo extends EntityStaticCar
18+
{
19+
public static function loadValidatorMetadata(ClassMetadata $metadata)
20+
{
21+
$metadata->addPropertyConstraint('wheels', new Length(array('max' => 99)));
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
use Symfony\Component\Validator\Mapping\ClassMetadata;
15+
use Symfony\Component\Validator\Constraints\Length;
16+
17+
class EntityStaticVehicle
18+
{
19+
public $wheels;
20+
21+
public static function loadValidatorMetadata(ClassMetadata $metadata)
22+
{
23+
$metadata->addPropertyConstraint('wheels', new Length(array('max' => 99)));
24+
}
25+
}

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

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

1414
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\Constraints\GreaterThan;
1615
use Symfony\Component\Validator\Constraints\Valid;
1716
use Symfony\Component\Validator\Mapping\ClassMetadata;
1817
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -304,21 +303,6 @@ public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadat
304303
{
305304
$this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
306305
}
307-
308-
public function testMergeDoesOverrideConstraintsFromParentClassIfPropertyIsOverriddenInChildClass()
309-
{
310-
$parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass');
311-
$parentMetadata->addPropertyConstraint('example', new GreaterThan(0));
312-
313-
$childMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass');
314-
$childMetadata->addPropertyConstraint('example', new GreaterThan(1));
315-
$childMetadata->mergeConstraints($parentMetadata);
316-
317-
$expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass');
318-
$expectedMetadata->addPropertyConstraint('example', new GreaterThan(1));
319-
320-
$this->assertEquals($expectedMetadata, $childMetadata);
321-
}
322306
}
323307

324308
class ParentClass

src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ public function testMetadataCacheWithRuntimeConstraint()
168168

169169
$metadata = $factory->getMetadataFor(self::CLASS_NAME);
170170
}
171+
172+
public function testGroupsFromParent()
173+
{
174+
$reader = new \Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader();
175+
$factory = new LazyLoadingMetadataFactory($reader);
176+
$metadata = $factory->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\EntityStaticCarTurbo');
177+
$groups = array();
178+
179+
foreach ($metadata->getPropertyMetadata('wheels') as $propertyMetadata) {
180+
$constraints = $propertyMetadata->getConstraints();
181+
$groups = array_replace($groups, $constraints[0]->groups);
182+
}
183+
184+
$this->assertCount(4, $groups);
185+
$this->assertContains('Default', $groups);
186+
$this->assertContains('EntityStaticCarTurbo', $groups);
187+
$this->assertContains('EntityStaticCar', $groups);
188+
$this->assertContains('EntityStaticVehicle', $groups);
189+
}
171190
}
172191

173192
class TestLoader implements LoaderInterface

0 commit comments

Comments
 (0)
0