Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitedcfd60

Browse files
committed
[Validator] Fixed calling getters before resolving groups
1 parenta562ba2 commitedcfd60

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

‎src/Symfony/Component/Validator/Context/ExecutionContext.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
useSymfony\Component\Validator\Mapping\MetadataInterface;
2121
useSymfony\Component\Validator\Mapping\PropertyMetadataInterface;
2222
useSymfony\Component\Validator\Util\PropertyPath;
23+
useSymfony\Component\Validator\Validator\LazyProperty;
2324
useSymfony\Component\Validator\Validator\ValidatorInterface;
2425
useSymfony\Component\Validator\Violation\ConstraintViolationBuilder;
2526

@@ -187,7 +188,7 @@ public function addViolation($message, array $parameters = [])
187188
$parameters,
188189
$this->root,
189190
$this->propertyPath,
190-
$this->value,
191+
$this->getValue(),
191192
null,
192193
null,
193194
$this->constraint
@@ -206,7 +207,7 @@ public function buildViolation($message, array $parameters = [])
206207
$parameters,
207208
$this->root,
208209
$this->propertyPath,
209-
$this->value,
210+
$this->getValue(),
210211
$this->translator,
211212
$this->translationDomain
212213
);
@@ -241,6 +242,10 @@ public function getRoot()
241242
*/
242243
publicfunctiongetValue()
243244
{
245+
if ($this->valueinstanceof LazyProperty) {
246+
return$this->value->getPropertyValue();
247+
}
248+
244249
return$this->value;
245250
}
246251

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
namespaceSymfony\Component\Validator\Tests\Fixtures;
13+
14+
class EntityWithGroupedConstraintOnMethods
15+
{
16+
public$bar;
17+
18+
publicfunctionisValidInFoo()
19+
{
20+
returnfalse;
21+
}
22+
23+
publicfunctiongetBar()
24+
{
25+
thrownew \Exception('Should not be called');
26+
}
27+
}

‎src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
useSymfony\Component\Translation\IdentityTranslator;
1515
useSymfony\Component\Validator\Constraints\All;
1616
useSymfony\Component\Validator\Constraints\Collection;
17+
useSymfony\Component\Validator\Constraints\GroupSequence;
18+
useSymfony\Component\Validator\Constraints\IsTrue;
1719
useSymfony\Component\Validator\Constraints\Length;
1820
useSymfony\Component\Validator\Constraints\NotBlank;
21+
useSymfony\Component\Validator\Constraints\NotNull;
1922
useSymfony\Component\Validator\ConstraintValidatorFactory;
2023
useSymfony\Component\Validator\Context\ExecutionContextFactory;
24+
useSymfony\Component\Validator\Mapping\ClassMetadata;
2125
useSymfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2226
useSymfony\Component\Validator\Tests\Constraints\Fixtures\ChildA;
2327
useSymfony\Component\Validator\Tests\Constraints\Fixtures\ChildB;
2428
useSymfony\Component\Validator\Tests\Fixtures\Entity;
29+
useSymfony\Component\Validator\Tests\Fixtures\EntityWithGroupedConstraintOnMethods;
2530
useSymfony\Component\Validator\Validator\RecursiveValidator;
2631

2732
class RecursiveValidatorTestextends AbstractTest
@@ -117,6 +122,25 @@ public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
117122
$this->assertInstanceOf(NotBlank::class,$violations->get(1)->getConstraint());
118123
}
119124

125+
publicfunctiontestGroupedMethodConstraintValidateInSequence()
126+
{
127+
$metadata =newClassMetadata(EntityWithGroupedConstraintOnMethods::class);
128+
$metadata->addPropertyConstraint('bar',newNotNull(['groups' =>'Foo']));
129+
$metadata->addGetterMethodConstraint('validInFoo','isValidInFoo',newIsTrue(['groups' =>'Foo']));
130+
$metadata->addGetterMethodConstraint('bar','getBar',newNotNull(['groups' =>'Bar']));
131+
132+
$this->metadataFactory->addMetadata($metadata);
133+
134+
$entity =newEntityWithGroupedConstraintOnMethods();
135+
$groups =newGroupSequence(['EntityWithGroupedConstraintOnMethods','Foo','Bar']);
136+
137+
$violations =$this->validator->validate($entity,null,$groups);
138+
139+
$this->assertCount(2,$violations);
140+
$this->assertInstanceOf(NotNull::class,$violations->get(0)->getConstraint());
141+
$this->assertInstanceOf(IsTrue::class,$violations->get(1)->getConstraint());
142+
}
143+
120144
publicfunctiontestAllConstraintValidateAllGroupsForNestedConstraints()
121145
{
122146
$this->metadata->addPropertyConstraint('data',newAll(['constraints' => [
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
namespaceSymfony\Component\Validator\Validator;
13+
14+
/**
15+
* A wrapper for a callable initializing a property from a getter.
16+
*
17+
* @internal
18+
*/
19+
class LazyProperty
20+
{
21+
private$propertyValueCallback;
22+
23+
publicfunction__construct(\Closure$propertyValueCallback)
24+
{
25+
$this->propertyValueCallback =$propertyValueCallback;
26+
}
27+
28+
publicfunctiongetPropertyValue()
29+
{
30+
return\call_user_func($this->propertyValueCallback);
31+
}
32+
}

‎src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
useSymfony\Component\Validator\Mapping\ClassMetadataInterface;
2727
useSymfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2828
useSymfony\Component\Validator\Mapping\GenericMetadata;
29+
useSymfony\Component\Validator\Mapping\GetterMetadata;
2930
useSymfony\Component\Validator\Mapping\MetadataInterface;
3031
useSymfony\Component\Validator\Mapping\PropertyMetadataInterface;
3132
useSymfony\Component\Validator\Mapping\TraversalStrategy;
@@ -534,7 +535,13 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
534535
thrownewUnsupportedMetadataException(sprintf('The property metadata instances should implement "Symfony\Component\Validator\Mapping\PropertyMetadataInterface", got: "%s".',\is_object($propertyMetadata) ?\get_class($propertyMetadata) :\gettype($propertyMetadata)));
535536
}
536537

537-
$propertyValue =$propertyMetadata->getPropertyValue($object);
538+
if ($propertyMetadatainstanceof GetterMetadata) {
539+
$propertyValue =newLazyProperty(staticfunction ()use ($propertyMetadata,$object) {
540+
return$propertyMetadata->getPropertyValue($object);
541+
});
542+
}else {
543+
$propertyValue =$propertyMetadata->getPropertyValue($object);
544+
}
538545

539546
$this->validateGenericNode(
540547
$propertyValue,
@@ -798,6 +805,11 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
798805

799806
$validator =$this->validatorFactory->getInstance($constraint);
800807
$validator->initialize($context);
808+
809+
if ($valueinstanceof LazyProperty) {
810+
$value =$value->getPropertyValue();
811+
}
812+
801813
$validator->validate($value,$constraint);
802814
}
803815
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp