Skip to content

[Validator] Backported #11410 to 2.3: Object initializers are called only once per object #11411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Entity extends EntityParent implements EntityInterface
public $reference;
private $internal;
public $data = 'Overridden data';
public $initialized = false;

public function __construct($internal = null)
{
Expand Down
48 changes: 48 additions & 0 deletions src/Symfony/Component/Validator/Tests/ValidationVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator\Tests;

use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\ExecutionContextInterface;
use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Tests\Fixtures\Reference;
Expand Down Expand Up @@ -561,4 +563,50 @@ public function testValidateCascadedPropertyRequiresObjectOrArray()

$this->visitor->validate($entity, 'Default', '');
}

public function testInitializeObjectsOnFirstValidation()
{
$test = $this;
$entity = new Entity();
$entity->initialized = false;

// prepare initializers that set "initialized" to true
$initializer1 = $this->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
$initializer2 = $this->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');

$initializer1->expects($this->once())
->method('initialize')
->with($entity)
->will($this->returnCallback(function ($object) {
$object->initialized = true;
}));

$initializer2->expects($this->once())
->method('initialize')
->with($entity);

$this->visitor = new ValidationVisitor('Root', $this->metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator(), null, array(
$initializer1,
$initializer2
));

// prepare constraint which
// * checks that "initialized" is set to true
// * validates the object again
$callback = function ($object, ExecutionContextInterface $context) use ($test) {
$test->assertTrue($object->initialized);

// validate again in same group
$context->validate($object);

// validate again in other group
$context->validate($object, '', 'SomeGroup');
};

$this->metadata->addConstraint(new Callback(array($callback)));

$this->visitor->validate($entity, 'Default', '');

$this->assertTrue($entity->initialized);
}
}
17 changes: 10 additions & 7 deletions src/Symfony/Component/Validator/ValidationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,19 @@ public function validate($value, $group, $propertyPath, $traverse = false, $deep
return;
}

// Initialize if the object wasn't initialized before
if (!isset($this->validatedObjects[$hash])) {
foreach ($this->objectInitializers as $initializer) {
if (!$initializer instanceof ObjectInitializerInterface) {
throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
}
$initializer->initialize($value);
}
}

// Remember validating this object before starting and possibly
// traversing the object graph
$this->validatedObjects[$hash][$group] = true;

foreach ($this->objectInitializers as $initializer) {
if (!$initializer instanceof ObjectInitializerInterface) {
throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
}
$initializer->initialize($value);
}
}

// Validate arrays recursively by default, otherwise every driver needs
Expand Down