Permalink
Browse files

fixed validation of Doctrine proxy objects

  • Loading branch information...
fabpot committed Jul 6, 2011
1 parent c72445c commit bd92919d029d296359f2accca053503b80ab256a
Showing with 45 additions and 3 deletions.
  1. +10 −1 GraphWalker.php
  2. +30 −0 ObjectInitializerInterface.php
  3. +5 −2 Validator.php
View
@@ -32,13 +32,15 @@ class GraphWalker
protected $context;
protected $validatorFactory;
protected $metadataFactory;
+ protected $validatorInitializers = array();
protected $validatedObjects = array();
- public function __construct($root, ClassMetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $factory)
+ public function __construct($root, ClassMetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $factory, array $validatorInitializers = array())
{
$this->context = new ExecutionContext($root, $this, $metadataFactory);
$this->validatorFactory = $factory;
$this->metadataFactory = $metadataFactory;
+ $this->validatorInitializers = $validatorInitializers;
}
/**
@@ -60,6 +62,13 @@ public function getViolations()
*/
public function walkObject(ClassMetadata $metadata, $object, $group, $propertyPath)
{
+ foreach ($this->validatorInitializers as $initializer) {
+ if (!$initializer instanceof ObjectInitializerInterface) {
+ throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
+ }
+ $initializer->initialize($object);
+ }
+
$this->context->setCurrentClass($metadata->getClassName());
if ($group === Constraint::DEFAULT_GROUP && $metadata->hasGroupSequence()) {
@@ -0,0 +1,30 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator;
+
+/**
+ * Interface for object initializers.
+ *
+ * Concrete implementations of this interface are used by the GraphWalker
+ * to initialize objects just before validating them/
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+interface ObjectInitializerInterface
+{
+ /**
+ * Initializes an object just before validation.
+ *
+ * @param object $object The object to validate
+ */
+ function initialize($object);
+}
View
@@ -28,14 +28,17 @@ class Validator implements ValidatorInterface
{
protected $metadataFactory;
protected $validatorFactory;
+ protected $validatorInitializers;
public function __construct(
ClassMetadataFactoryInterface $metadataFactory,
- ConstraintValidatorFactoryInterface $validatorFactory
+ ConstraintValidatorFactoryInterface $validatorFactory,
+ array $validatorInitializers = array()
)
{
$this->metadataFactory = $metadataFactory;
$this->validatorFactory = $validatorFactory;
+ $this->validatorInitializers = $validatorInitializers;
}
/**
@@ -102,7 +105,7 @@ public function validateValue($value, Constraint $constraint, $groups = null)
protected function validateGraph($root, \Closure $walk, $groups = null)
{
- $walker = new GraphWalker($root, $this->metadataFactory, $this->validatorFactory);
+ $walker = new GraphWalker($root, $this->metadataFactory, $this->validatorFactory, $this->validatorInitializers);
$groups = $groups ? (array) $groups : array(Constraint::DEFAULT_GROUP);
foreach ($groups as $group) {

0 comments on commit bd92919

Please sign in to comment.