8000 merged branch bschussek/drupal-validator (PR #6096) · symfony/symfony@ee90986 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee90986

Browse files
committed
merged branch bschussek/drupal-validator (PR #6096)
This PR was merged into the master branch. Commits ------- 1858b96 [Form] Adapted FormValidator to latest changes in the Validator 1f752e8 [DoctrineBridge] Adapted UniqueValidator to latest changes in the Validator efe42cb [Validator] Refactored the GraphWalker into an implementation of the Visitor design pattern. Discussion ---------- [Validator] Refactored the Validator for use in Drupal Bug fix: no Feature addition: no Backwards compatibility break: yes Symfony2 tests pass: yes Fixes the following tickets: - Todo: - License of the code: MIT Documentation PR: TODO Drupal wants to use the Symfony Validator component in their next version. I was talking to @fago recently about the changes that we'd need to make and implemented these changes in this PR. I don't want to rush this, but the deadline is tight, since Drupal feature freeze is on December 1st and @fago needs at least a couple of days to integrate the Validator into Drupal. This PR introduces two significant changes: * Interfaces were created for all classes that constitute the Validator's API. This is were the PR breaks BC, because `ConstraintValidatorInterface::initialize()` is now type hinted against `ExecutionContextInterface` instead of `ExecutionContext`. * The graph walker was refactored into an implementation of the Visitor pattern. This way, the validator was decoupled from the structure of the metadata (class → properties and getter methods) and makes it possible to implement a different metadata structure, as is required by the Drupal Entity API. As a consequence of the API change, custom validation code is now much easier to write, because `ValidatorInterface` and `ExecutionContextInterface` share the following set of methods: ```php interface ValidatorInterface { public function validate($value, $groups = null, $traverse = false, $deep = false); public function validateValue($value, $constraints, $groups = null); public function getMetadataFor($value); } interface ExecutionContextInterface { public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false); public function validateValue($value, $constraints, $subPath = '', $groups = null); public function getMetadataFor($value); } ``` No more juggling with property paths, no more fiddling with the graph walker. Just call on the execution context what you'd call on the validator and you're done. There are two controversial things to discuss and decide (cc @fabpot): * I moved the `@api` tags of all implementations to the respective interfaces. Is this ok? * I would like to deprecate `ValidatorInterface::getMetadataFactory()` (tagged as `@api`) in favor of the added `ValidatorInterface::getMetadataFor()`, which offers the exact same functionality, but with a different API and better encapsulation, which makes it easier to maintain for us. We can tag `getMetadataFor()` as `@api`, as I don't expect it to change. Can we do this or should we leave the old method in? I would like to decide the major issues of this PR until **Sunday November 25th** in order to give @fago enough room for his implementation. Let me hear your thoughts.
2 parents d5ff238 + 1858b96 commit ee90986

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2849
-734
lines changed

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\Validator\Constraints;
1313

1414
use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase;
15+
use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
1516
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity;
1617
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity;
1718
use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity;
@@ -93,17 +94,6 @@ protected function createEntityManagerMock($repositoryMock)
9394
return $em;
9495
}
9596

96-
protected function createMetadataFactoryMock($metadata)
97-
{
98-
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
99-
$metadataFactory->expects($this->any())
100-
->method('getClassMetadata')
101-
->with($this->equalTo($metadata->name))
102-
->will($this->returnValue($metadata));
103-
104-
return $metadataFactory;
105-
}
106-
10797
protected function createValidatorFactory($uniqueValidator)
10898
{
10999
$validatorFactory = $this->getMock('Symfony\Component\Validator\ConstraintValidatorFactoryInterface');
@@ -138,7 +128,8 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
138128
));
139129
$metadata->addConstraint($constraint);
140130

141-
$metadataFactory = $this->createMetadataFactoryMock($metadata);
131+
$metadataFactory = new FakeMetadataFactory();
132+
$metadataFactory->addMetadata($metadata);
142133
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
143134

144135
return new Validator($metadataFactory, $validatorFactory);

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,12 @@ public function validate($form, Constraint $constraint)
5151

5252
if ($form->isSynchronized()) {
5353
// Validate the form data only if transformation succeeded
54-
$path = $this->context->getPropertyPath();
55-
$graphWalker = $this->context->getGraphWalker();
5654
$groups = self::getValidationGroups($form);
5755

58-
if (!empty($path)) {
59-
$path .= '.';
60-
}
61-
6256
// Validate the data against its own constraints
6357
if (self::allowDataWalking($form)) {
6458
foreach ($groups as $group) {
65-
$graphWalker->walkReference($form->getData(), $group, $path . 'data', true);
59+
$this->context->validate($form->getData(), 'data', $group, true);
6660
}
6761
}
6862

@@ -72,7 +66,7 @@ public function validate($form, Constraint $constraint)
7266
foreach ($constraints as $constraint) {
7367
foreach ($groups as $group) {
7468
if (in_array($group, $constraint->groups)) {
75-
$graphWalker->walkConstraint($constraint, $form->getData(), $group, $path . 'data');
69+
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
7670

7771
// Prevent duplicate validation
7872
continue 2;

0 commit comments

Comments
 (0)
0